1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S --
9 -- Copyright (C) 2001-2009, 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 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with Ada.Streams; use Ada.Streams;
35 with Ada.Exceptions; use Ada.Exceptions;
36 with Ada.Finalization;
37 with Ada.Unchecked_Conversion;
39 with Interfaces.C.Strings;
41 with GNAT.Sockets.Thin_Common; use GNAT.Sockets.Thin_Common;
42 with GNAT.Sockets.Thin; use GNAT.Sockets.Thin;
43 with GNAT.Sockets.Thin.Task_Safe_NetDB; use GNAT.Sockets.Thin.Task_Safe_NetDB;
45 with GNAT.Sockets.Linker_Options;
46 pragma Warnings (Off, GNAT.Sockets.Linker_Options);
47 -- Need to include pragma Linker_Options which is platform dependent
49 with System; use System;
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 -- The network database functions gethostbyname, gethostbyaddr,
61 -- getservbyname and getservbyport can either be guaranteed task safe by
62 -- the operating system, or else return data through a user-provided buffer
63 -- to ensure concurrent uses do not interfere.
65 -- Correspondence tables
67 Levels : constant array (Level_Type) of C.int :=
68 (Socket_Level => SOSC.SOL_SOCKET,
69 IP_Protocol_For_IP_Level => SOSC.IPPROTO_IP,
70 IP_Protocol_For_UDP_Level => SOSC.IPPROTO_UDP,
71 IP_Protocol_For_TCP_Level => SOSC.IPPROTO_TCP);
73 Modes : constant array (Mode_Type) of C.int :=
74 (Socket_Stream => SOSC.SOCK_STREAM,
75 Socket_Datagram => SOSC.SOCK_DGRAM);
77 Shutmodes : constant array (Shutmode_Type) of C.int :=
78 (Shut_Read => SOSC.SHUT_RD,
79 Shut_Write => SOSC.SHUT_WR,
80 Shut_Read_Write => SOSC.SHUT_RDWR);
82 Requests : constant array (Request_Name) of C.int :=
83 (Non_Blocking_IO => SOSC.FIONBIO,
84 N_Bytes_To_Read => SOSC.FIONREAD);
86 Options : constant array (Option_Name) of C.int :=
87 (Keep_Alive => SOSC.SO_KEEPALIVE,
88 Reuse_Address => SOSC.SO_REUSEADDR,
89 Broadcast => SOSC.SO_BROADCAST,
90 Send_Buffer => SOSC.SO_SNDBUF,
91 Receive_Buffer => SOSC.SO_RCVBUF,
92 Linger => SOSC.SO_LINGER,
93 Error => SOSC.SO_ERROR,
94 No_Delay => SOSC.TCP_NODELAY,
95 Add_Membership => SOSC.IP_ADD_MEMBERSHIP,
96 Drop_Membership => SOSC.IP_DROP_MEMBERSHIP,
97 Multicast_If => SOSC.IP_MULTICAST_IF,
98 Multicast_TTL => SOSC.IP_MULTICAST_TTL,
99 Multicast_Loop => SOSC.IP_MULTICAST_LOOP,
100 Receive_Packet_Info => SOSC.IP_PKTINFO,
101 Send_Timeout => SOSC.SO_SNDTIMEO,
102 Receive_Timeout => SOSC.SO_RCVTIMEO);
103 -- ??? Note: for OpenSolaris, Receive_Packet_Info should be IP_RECVPKTINFO,
104 -- but for Linux compatibility this constant is the same as IP_PKTINFO.
106 Flags : constant array (0 .. 3) of C.int :=
107 (0 => SOSC.MSG_OOB, -- Process_Out_Of_Band_Data
108 1 => SOSC.MSG_PEEK, -- Peek_At_Incoming_Data
109 2 => SOSC.MSG_WAITALL, -- Wait_For_A_Full_Reception
110 3 => SOSC.MSG_EOR); -- Send_End_Of_Record
112 Socket_Error_Id : constant Exception_Id := Socket_Error'Identity;
113 Host_Error_Id : constant Exception_Id := Host_Error'Identity;
115 Hex_To_Char : constant String (1 .. 16) := "0123456789ABCDEF";
116 -- Use to print in hexadecimal format
118 -----------------------
119 -- Local subprograms --
120 -----------------------
122 function Resolve_Error
123 (Error_Value : Integer;
124 From_Errno : Boolean := True) return Error_Type;
125 -- Associate an enumeration value (error_type) to en error value (errno).
126 -- From_Errno prevents from mixing h_errno with errno.
128 function To_Name (N : String) return Name_Type;
129 function To_String (HN : Name_Type) return String;
130 -- Conversion functions
132 function To_Int (F : Request_Flag_Type) return C.int;
133 -- Return the int value corresponding to the specified flags combination
135 function Set_Forced_Flags (F : C.int) return C.int;
136 -- Return F with the bits from SOSC.MSG_Forced_Flags forced set
138 function Short_To_Network
139 (S : C.unsigned_short) return C.unsigned_short;
140 pragma Inline (Short_To_Network);
141 -- Convert a port number into a network port number
143 function Network_To_Short
144 (S : C.unsigned_short) return C.unsigned_short
145 renames Short_To_Network;
146 -- Symmetric operation
149 (Val : Inet_Addr_VN_Type;
150 Hex : Boolean := False) return String;
151 -- Output an array of inet address components in hex or decimal mode
153 function Is_IP_Address (Name : String) return Boolean;
154 -- Return true when Name is an IP address in standard dot notation
156 function To_In_Addr (Addr : Inet_Addr_Type) return In_Addr;
157 procedure To_Inet_Addr
159 Result : out Inet_Addr_Type);
160 -- Conversion functions
162 function To_Host_Entry (E : Hostent) return Host_Entry_Type;
163 -- Conversion function
165 function To_Service_Entry (E : Servent) return Service_Entry_Type;
166 -- Conversion function
168 function To_Timeval (Val : Timeval_Duration) return Timeval;
169 -- Separate Val in seconds and microseconds
171 function To_Duration (Val : Timeval) return Timeval_Duration;
172 -- Reconstruct a Duration value from a Timeval record (seconds and
175 procedure Raise_Socket_Error (Error : Integer);
176 -- Raise Socket_Error with an exception message describing the error code
179 procedure Raise_Host_Error (H_Error : Integer);
180 -- Raise Host_Error exception with message describing error code (note
181 -- hstrerror seems to be obsolete) from h_errno.
183 procedure Narrow (Item : in out Socket_Set_Type);
184 -- Update Last as it may be greater than the real last socket
186 -- Types needed for Datagram_Socket_Stream_Type
188 type Datagram_Socket_Stream_Type is new Root_Stream_Type with record
189 Socket : Socket_Type;
191 From : Sock_Addr_Type;
194 type Datagram_Socket_Stream_Access is
195 access all Datagram_Socket_Stream_Type;
198 (Stream : in out Datagram_Socket_Stream_Type;
199 Item : out Ada.Streams.Stream_Element_Array;
200 Last : out Ada.Streams.Stream_Element_Offset);
203 (Stream : in out Datagram_Socket_Stream_Type;
204 Item : Ada.Streams.Stream_Element_Array);
206 -- Types needed for Stream_Socket_Stream_Type
208 type Stream_Socket_Stream_Type is new Root_Stream_Type with record
209 Socket : Socket_Type;
212 type Stream_Socket_Stream_Access is
213 access all Stream_Socket_Stream_Type;
216 (Stream : in out Stream_Socket_Stream_Type;
217 Item : out Ada.Streams.Stream_Element_Array;
218 Last : out Ada.Streams.Stream_Element_Offset);
221 (Stream : in out Stream_Socket_Stream_Type;
222 Item : Ada.Streams.Stream_Element_Array);
224 procedure Stream_Write
225 (Socket : Socket_Type;
226 Item : Ada.Streams.Stream_Element_Array;
227 To : access Sock_Addr_Type);
228 -- Common implementation for the Write operation of Datagram_Socket_Stream_
229 -- Type and Stream_Socket_Stream_Type.
231 procedure Wait_On_Socket
232 (Socket : Socket_Type;
234 Timeout : Selector_Duration;
235 Selector : access Selector_Type := null;
236 Status : out Selector_Status);
237 -- Common code for variants of socket operations supporting a timeout:
238 -- block in Check_Selector on Socket for at most the indicated timeout.
239 -- If For_Read is True, Socket is added to the read set for this call, else
240 -- it is added to the write set. If no selector is provided, a local one is
241 -- created for this call and destroyed prior to returning.
243 type Sockets_Library_Controller is new Ada.Finalization.Limited_Controlled
245 -- This type is used to generate automatic calls to Initialize and Finalize
246 -- during the elaboration and finalization of this package. A single object
247 -- of this type must exist at library level.
249 function Err_Code_Image (E : Integer) return String;
250 -- Return the value of E surrounded with brackets
253 (First : Stream_Element_Offset;
254 Count : C.int) return Stream_Element_Offset;
255 -- Compute the Last OUT parameter for the various Receive_Socket
256 -- subprograms: returns First + Count - 1, except for the case
257 -- where First = Stream_Element_Offset'First and Res = 0, in which
258 -- case Stream_Element_Offset'Last is returned instead.
260 procedure Initialize (X : in out Sockets_Library_Controller);
261 procedure Finalize (X : in out Sockets_Library_Controller);
263 procedure Normalize_Empty_Socket_Set (S : in out Socket_Set_Type);
264 -- If S is the empty set (detected by Last = No_Socket), make sure its
265 -- fd_set component is actually cleared. Note that the case where it is
266 -- not can occur for an uninitialized Socket_Set_Type object.
268 function Is_Open (S : Selector_Type) return Boolean;
269 -- Return True for an "open" Selector_Type object, i.e. one for which
270 -- Create_Selector has been called and Close_Selector has not been called.
276 function "+" (L, R : Request_Flag_Type) return Request_Flag_Type is
285 procedure Abort_Selector (Selector : Selector_Type) is
289 if not Is_Open (Selector) then
290 raise Program_Error with "closed selector";
293 -- Send one byte to unblock select system call
295 Res := Signalling_Fds.Write (C.int (Selector.W_Sig_Socket));
297 if Res = Failure then
298 Raise_Socket_Error (Socket_Errno);
306 procedure Accept_Socket
307 (Server : Socket_Type;
308 Socket : out Socket_Type;
309 Address : out Sock_Addr_Type)
312 Sin : aliased Sockaddr_In;
313 Len : aliased C.int := Sin'Size / 8;
316 Res := C_Accept (C.int (Server), Sin'Address, Len'Access);
318 if Res = Failure then
319 Raise_Socket_Error (Socket_Errno);
322 Socket := Socket_Type (Res);
324 To_Inet_Addr (Sin.Sin_Addr, Address.Addr);
325 Address.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
332 procedure Accept_Socket
333 (Server : Socket_Type;
334 Socket : out Socket_Type;
335 Address : out Sock_Addr_Type;
336 Timeout : Selector_Duration;
337 Selector : access Selector_Type := null;
338 Status : out Selector_Status)
341 if Selector /= null and then not Is_Open (Selector.all) then
342 raise Program_Error with "closed selector";
345 -- Wait for socket to become available for reading
351 Selector => Selector,
354 -- Accept connection if available
356 if Status = Completed then
357 Accept_Socket (Server, Socket, Address);
368 (E : Host_Entry_Type;
369 N : Positive := 1) return Inet_Addr_Type
372 return E.Addresses (N);
375 ----------------------
376 -- Addresses_Length --
377 ----------------------
379 function Addresses_Length (E : Host_Entry_Type) return Natural is
381 return E.Addresses_Length;
382 end Addresses_Length;
389 (E : Host_Entry_Type;
390 N : Positive := 1) return String
393 return To_String (E.Aliases (N));
401 (S : Service_Entry_Type;
402 N : Positive := 1) return String
405 return To_String (S.Aliases (N));
412 function Aliases_Length (E : Host_Entry_Type) return Natural is
414 return E.Aliases_Length;
421 function Aliases_Length (S : Service_Entry_Type) return Natural is
423 return S.Aliases_Length;
430 procedure Bind_Socket
431 (Socket : Socket_Type;
432 Address : Sock_Addr_Type)
435 Sin : aliased Sockaddr_In;
436 Len : constant C.int := Sin'Size / 8;
437 -- This assumes that Address.Family = Family_Inet???
440 if Address.Family = Family_Inet6 then
441 raise Socket_Error with "IPv6 not supported";
444 Set_Family (Sin.Sin_Family, Address.Family);
445 Set_Address (Sin'Unchecked_Access, To_In_Addr (Address.Addr));
447 (Sin'Unchecked_Access,
448 Short_To_Network (C.unsigned_short (Address.Port)));
450 Res := C_Bind (C.int (Socket), Sin'Address, Len);
452 if Res = Failure then
453 Raise_Socket_Error (Socket_Errno);
461 procedure Check_Selector
462 (Selector : in out Selector_Type;
463 R_Socket_Set : in out Socket_Set_Type;
464 W_Socket_Set : in out Socket_Set_Type;
465 Status : out Selector_Status;
466 Timeout : Selector_Duration := Forever)
468 E_Socket_Set : Socket_Set_Type;
471 (Selector, R_Socket_Set, W_Socket_Set, E_Socket_Set, Status, Timeout);
478 procedure Check_Selector
479 (Selector : in out Selector_Type;
480 R_Socket_Set : in out Socket_Set_Type;
481 W_Socket_Set : in out Socket_Set_Type;
482 E_Socket_Set : in out Socket_Set_Type;
483 Status : out Selector_Status;
484 Timeout : Selector_Duration := Forever)
488 RSig : constant Socket_Type := Selector.R_Sig_Socket;
489 TVal : aliased Timeval;
490 TPtr : Timeval_Access;
493 if not Is_Open (Selector) then
494 raise Program_Error with "closed selector";
499 -- No timeout or Forever is indicated by a null timeval pointer
501 if Timeout = Forever then
504 TVal := To_Timeval (Timeout);
505 TPtr := TVal'Unchecked_Access;
508 -- Add read signalling socket
510 Set (R_Socket_Set, RSig);
512 Last := C.int'Max (C.int'Max (C.int (R_Socket_Set.Last),
513 C.int (W_Socket_Set.Last)),
514 C.int (E_Socket_Set.Last));
516 -- Zero out fd_set for empty Socket_Set_Type objects
518 Normalize_Empty_Socket_Set (R_Socket_Set);
519 Normalize_Empty_Socket_Set (W_Socket_Set);
520 Normalize_Empty_Socket_Set (E_Socket_Set);
525 R_Socket_Set.Set'Access,
526 W_Socket_Set.Set'Access,
527 E_Socket_Set.Set'Access,
530 if Res = Failure then
531 Raise_Socket_Error (Socket_Errno);
534 -- If Select was resumed because of read signalling socket, read this
535 -- data and remove socket from set.
537 if Is_Set (R_Socket_Set, RSig) then
538 Clear (R_Socket_Set, RSig);
540 Res := Signalling_Fds.Read (C.int (RSig));
542 if Res = Failure then
543 Raise_Socket_Error (Socket_Errno);
552 -- Update socket sets in regard to their new contents
554 Narrow (R_Socket_Set);
555 Narrow (W_Socket_Set);
556 Narrow (E_Socket_Set);
564 (Item : in out Socket_Set_Type;
565 Socket : Socket_Type)
567 Last : aliased C.int := C.int (Item.Last);
569 if Item.Last /= No_Socket then
570 Remove_Socket_From_Set (Item.Set'Access, C.int (Socket));
571 Last_Socket_In_Set (Item.Set'Access, Last'Unchecked_Access);
572 Item.Last := Socket_Type (Last);
580 procedure Close_Selector (Selector : in out Selector_Type) is
582 if not Is_Open (Selector) then
584 -- Selector already in closed state: nothing to do
589 -- Close the signalling file descriptors used internally for the
590 -- implementation of Abort_Selector.
592 Signalling_Fds.Close (C.int (Selector.R_Sig_Socket));
593 Signalling_Fds.Close (C.int (Selector.W_Sig_Socket));
595 -- Reset R_Sig_Socket and W_Sig_Socket to No_Socket to ensure that any
596 -- (erroneous) subsequent attempt to use this selector properly fails.
598 Selector.R_Sig_Socket := No_Socket;
599 Selector.W_Sig_Socket := No_Socket;
606 procedure Close_Socket (Socket : Socket_Type) is
610 Res := C_Close (C.int (Socket));
612 if Res = Failure then
613 Raise_Socket_Error (Socket_Errno);
621 procedure Connect_Socket
622 (Socket : Socket_Type;
623 Server : Sock_Addr_Type)
626 Sin : aliased Sockaddr_In;
627 Len : constant C.int := Sin'Size / 8;
630 if Server.Family = Family_Inet6 then
631 raise Socket_Error with "IPv6 not supported";
634 Set_Family (Sin.Sin_Family, Server.Family);
635 Set_Address (Sin'Unchecked_Access, To_In_Addr (Server.Addr));
637 (Sin'Unchecked_Access,
638 Short_To_Network (C.unsigned_short (Server.Port)));
640 Res := C_Connect (C.int (Socket), Sin'Address, Len);
642 if Res = Failure then
643 Raise_Socket_Error (Socket_Errno);
651 procedure Connect_Socket
652 (Socket : Socket_Type;
653 Server : Sock_Addr_Type;
654 Timeout : Selector_Duration;
655 Selector : access Selector_Type := null;
656 Status : out Selector_Status)
659 -- Used to set Socket to non-blocking I/O
662 if Selector /= null and then not Is_Open (Selector.all) then
663 raise Program_Error with "closed selector";
666 -- Set the socket to non-blocking I/O
668 Req := (Name => Non_Blocking_IO, Enabled => True);
669 Control_Socket (Socket, Request => Req);
671 -- Start operation (non-blocking), will raise Socket_Error with
675 Connect_Socket (Socket, Server);
677 when E : Socket_Error =>
678 if Resolve_Exception (E) = Operation_Now_In_Progress then
685 -- Wait for socket to become available for writing
691 Selector => Selector,
694 -- Reset the socket to blocking I/O
696 Req := (Name => Non_Blocking_IO, Enabled => False);
697 Control_Socket (Socket, Request => Req);
704 procedure Control_Socket
705 (Socket : Socket_Type;
706 Request : in out Request_Type)
713 when Non_Blocking_IO =>
714 Arg := C.int (Boolean'Pos (Request.Enabled));
716 when N_Bytes_To_Read =>
721 (C.int (Socket), Requests (Request.Name), Arg'Unchecked_Access);
723 if Res = Failure then
724 Raise_Socket_Error (Socket_Errno);
728 when Non_Blocking_IO =>
731 when N_Bytes_To_Read =>
732 Request.Size := Natural (Arg);
741 (Source : Socket_Set_Type;
742 Target : out Socket_Set_Type)
748 ---------------------
749 -- Create_Selector --
750 ---------------------
752 procedure Create_Selector (Selector : out Selector_Type) is
753 Two_Fds : aliased Fd_Pair;
757 if Is_Open (Selector) then
758 -- Raise exception to prevent socket descriptor leak
760 raise Program_Error with "selector already open";
763 -- We open two signalling file descriptors. One of them is used to send
764 -- data to the other, which is included in a C_Select socket set. The
765 -- communication is used to force a call to C_Select to complete, and
766 -- the waiting task to resume its execution.
768 Res := Signalling_Fds.Create (Two_Fds'Access);
770 if Res = Failure then
771 Raise_Socket_Error (Socket_Errno);
774 Selector.R_Sig_Socket := Socket_Type (Two_Fds (Read_End));
775 Selector.W_Sig_Socket := Socket_Type (Two_Fds (Write_End));
782 procedure Create_Socket
783 (Socket : out Socket_Type;
784 Family : Family_Type := Family_Inet;
785 Mode : Mode_Type := Socket_Stream)
790 Res := C_Socket (Families (Family), Modes (Mode), 0);
792 if Res = Failure then
793 Raise_Socket_Error (Socket_Errno);
796 Socket := Socket_Type (Res);
803 procedure Empty (Item : out Socket_Set_Type) is
805 Reset_Socket_Set (Item.Set'Access);
806 Item.Last := No_Socket;
813 function Err_Code_Image (E : Integer) return String is
814 Msg : String := E'Img & "] ";
816 Msg (Msg'First) := '[';
824 procedure Finalize (X : in out Sockets_Library_Controller) is
825 pragma Unreferenced (X);
828 -- Finalization operation for the GNAT.Sockets package
837 procedure Finalize is
839 -- This is a dummy placeholder for an obsolete API.
840 -- The real finalization actions are in Initialize primitive operation
841 -- of Sockets_Library_Controller.
851 (Item : in out Socket_Set_Type;
852 Socket : out Socket_Type)
855 L : aliased C.int := C.int (Item.Last);
858 if Item.Last /= No_Socket then
860 (Item.Set'Access, Last => L'Access, Socket => S'Access);
861 Item.Last := Socket_Type (L);
862 Socket := Socket_Type (S);
873 (Stream : not null Stream_Access) return Sock_Addr_Type
876 if Stream.all in Datagram_Socket_Stream_Type then
877 return Datagram_Socket_Stream_Type (Stream.all).From;
879 return Get_Peer_Name (Stream_Socket_Stream_Type (Stream.all).Socket);
883 -------------------------
884 -- Get_Host_By_Address --
885 -------------------------
887 function Get_Host_By_Address
888 (Address : Inet_Addr_Type;
889 Family : Family_Type := Family_Inet) return Host_Entry_Type
891 pragma Unreferenced (Family);
893 HA : aliased In_Addr := To_In_Addr (Address);
894 Buflen : constant C.int := Netdb_Buffer_Size;
895 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
896 Res : aliased Hostent;
900 if Safe_Gethostbyaddr (HA'Address, HA'Size / 8, SOSC.AF_INET,
901 Res'Access, Buf'Address, Buflen, Err'Access) /= 0
903 Raise_Host_Error (Integer (Err));
906 return To_Host_Entry (Res);
907 end Get_Host_By_Address;
909 ----------------------
910 -- Get_Host_By_Name --
911 ----------------------
913 function Get_Host_By_Name (Name : String) return Host_Entry_Type is
915 -- Detect IP address name and redirect to Inet_Addr
917 if Is_IP_Address (Name) then
918 return Get_Host_By_Address (Inet_Addr (Name));
922 HN : constant C.char_array := C.To_C (Name);
923 Buflen : constant C.int := Netdb_Buffer_Size;
924 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
925 Res : aliased Hostent;
929 if Safe_Gethostbyname
930 (HN, Res'Access, Buf'Address, Buflen, Err'Access) /= 0
932 Raise_Host_Error (Integer (Err));
935 return To_Host_Entry (Res);
937 end Get_Host_By_Name;
943 function Get_Peer_Name (Socket : Socket_Type) return Sock_Addr_Type is
944 Sin : aliased Sockaddr_In;
945 Len : aliased C.int := Sin'Size / 8;
946 Res : Sock_Addr_Type (Family_Inet);
949 if C_Getpeername (C.int (Socket), Sin'Address, Len'Access) = Failure then
950 Raise_Socket_Error (Socket_Errno);
953 To_Inet_Addr (Sin.Sin_Addr, Res.Addr);
954 Res.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
959 -------------------------
960 -- Get_Service_By_Name --
961 -------------------------
963 function Get_Service_By_Name
965 Protocol : String) return Service_Entry_Type
967 SN : constant C.char_array := C.To_C (Name);
968 SP : constant C.char_array := C.To_C (Protocol);
969 Buflen : constant C.int := Netdb_Buffer_Size;
970 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
971 Res : aliased Servent;
974 if Safe_Getservbyname (SN, SP, Res'Access, Buf'Address, Buflen) /= 0 then
975 raise Service_Error with "Service not found";
978 -- Translate from the C format to the API format
980 return To_Service_Entry (Res);
981 end Get_Service_By_Name;
983 -------------------------
984 -- Get_Service_By_Port --
985 -------------------------
987 function Get_Service_By_Port
989 Protocol : String) return Service_Entry_Type
991 SP : constant C.char_array := C.To_C (Protocol);
992 Buflen : constant C.int := Netdb_Buffer_Size;
993 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
994 Res : aliased Servent;
997 if Safe_Getservbyport
998 (C.int (Short_To_Network (C.unsigned_short (Port))), SP,
999 Res'Access, Buf'Address, Buflen) /= 0
1001 raise Service_Error with "Service not found";
1004 -- Translate from the C format to the API format
1006 return To_Service_Entry (Res);
1007 end Get_Service_By_Port;
1009 ---------------------
1010 -- Get_Socket_Name --
1011 ---------------------
1013 function Get_Socket_Name
1014 (Socket : Socket_Type) return Sock_Addr_Type
1016 Sin : aliased Sockaddr_In;
1017 Len : aliased C.int := Sin'Size / 8;
1019 Addr : Sock_Addr_Type := No_Sock_Addr;
1022 Res := C_Getsockname (C.int (Socket), Sin'Address, Len'Access);
1024 if Res /= Failure then
1025 To_Inet_Addr (Sin.Sin_Addr, Addr.Addr);
1026 Addr.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
1030 end Get_Socket_Name;
1032 -----------------------
1033 -- Get_Socket_Option --
1034 -----------------------
1036 function Get_Socket_Option
1037 (Socket : Socket_Type;
1038 Level : Level_Type := Socket_Level;
1039 Name : Option_Name) return Option_Type
1041 use type C.unsigned_char;
1043 V8 : aliased Two_Ints;
1045 V1 : aliased C.unsigned_char;
1046 VT : aliased Timeval;
1047 Len : aliased C.int;
1048 Add : System.Address;
1050 Opt : Option_Type (Name);
1054 when Multicast_Loop |
1056 Receive_Packet_Info =>
1091 if Res = Failure then
1092 Raise_Socket_Error (Socket_Errno);
1100 Opt.Enabled := (V4 /= 0);
1103 Opt.Enabled := (V8 (V8'First) /= 0);
1104 Opt.Seconds := Natural (V8 (V8'Last));
1108 Opt.Size := Natural (V4);
1111 Opt.Error := Resolve_Error (Integer (V4));
1113 when Add_Membership |
1115 To_Inet_Addr (To_In_Addr (V8 (V8'First)), Opt.Multicast_Address);
1116 To_Inet_Addr (To_In_Addr (V8 (V8'Last)), Opt.Local_Interface);
1118 when Multicast_If =>
1119 To_Inet_Addr (To_In_Addr (V4), Opt.Outgoing_If);
1121 when Multicast_TTL =>
1122 Opt.Time_To_Live := Integer (V1);
1124 when Multicast_Loop |
1125 Receive_Packet_Info =>
1126 Opt.Enabled := (V1 /= 0);
1130 Opt.Timeout := To_Duration (VT);
1134 end Get_Socket_Option;
1140 function Host_Name return String is
1141 Name : aliased C.char_array (1 .. 64);
1145 Res := C_Gethostname (Name'Address, Name'Length);
1147 if Res = Failure then
1148 Raise_Socket_Error (Socket_Errno);
1151 return C.To_Ada (Name);
1159 (Val : Inet_Addr_VN_Type;
1160 Hex : Boolean := False) return String
1162 -- The largest Inet_Addr_Comp_Type image occurs with IPv4. It
1163 -- has at most a length of 3 plus one '.' character.
1165 Buffer : String (1 .. 4 * Val'Length);
1166 Length : Natural := 1;
1167 Separator : Character;
1169 procedure Img10 (V : Inet_Addr_Comp_Type);
1170 -- Append to Buffer image of V in decimal format
1172 procedure Img16 (V : Inet_Addr_Comp_Type);
1173 -- Append to Buffer image of V in hexadecimal format
1179 procedure Img10 (V : Inet_Addr_Comp_Type) is
1180 Img : constant String := V'Img;
1181 Len : constant Natural := Img'Length - 1;
1183 Buffer (Length .. Length + Len - 1) := Img (2 .. Img'Last);
1184 Length := Length + Len;
1191 procedure Img16 (V : Inet_Addr_Comp_Type) is
1193 Buffer (Length) := Hex_To_Char (Natural (V / 16) + 1);
1194 Buffer (Length + 1) := Hex_To_Char (Natural (V mod 16) + 1);
1195 Length := Length + 2;
1198 -- Start of processing for Image
1201 Separator := (if Hex then ':' else '.');
1203 for J in Val'Range loop
1210 if J /= Val'Last then
1211 Buffer (Length) := Separator;
1212 Length := Length + 1;
1216 return Buffer (1 .. Length - 1);
1223 function Image (Value : Inet_Addr_Type) return String is
1225 if Value.Family = Family_Inet then
1226 return Image (Inet_Addr_VN_Type (Value.Sin_V4), Hex => False);
1228 return Image (Inet_Addr_VN_Type (Value.Sin_V6), Hex => True);
1236 function Image (Value : Sock_Addr_Type) return String is
1237 Port : constant String := Value.Port'Img;
1239 return Image (Value.Addr) & ':' & Port (2 .. Port'Last);
1246 function Image (Socket : Socket_Type) return String is
1255 function Image (Item : Socket_Set_Type) return String is
1256 Socket_Set : Socket_Set_Type := Item;
1260 Last_Img : constant String := Socket_Set.Last'Img;
1262 (1 .. (Integer (Socket_Set.Last) + 1) * Last_Img'Length);
1263 Index : Positive := 1;
1264 Socket : Socket_Type;
1267 while not Is_Empty (Socket_Set) loop
1268 Get (Socket_Set, Socket);
1271 Socket_Img : constant String := Socket'Img;
1273 Buffer (Index .. Index + Socket_Img'Length - 1) := Socket_Img;
1274 Index := Index + Socket_Img'Length;
1278 return "[" & Last_Img & "]" & Buffer (1 .. Index - 1);
1286 function Inet_Addr (Image : String) return Inet_Addr_Type is
1288 use Interfaces.C.Strings;
1290 Img : aliased char_array := To_C (Image);
1291 Cp : constant chars_ptr := To_Chars_Ptr (Img'Unchecked_Access);
1292 Addr : aliased C.int;
1294 Result : Inet_Addr_Type;
1297 -- Special case for an empty Image as on some platforms (e.g. Windows)
1298 -- calling Inet_Addr("") will not return an error.
1301 Raise_Socket_Error (SOSC.EINVAL);
1304 Res := Inet_Pton (SOSC.AF_INET, Cp, Addr'Address);
1307 Raise_Socket_Error (Socket_Errno);
1310 Raise_Socket_Error (SOSC.EINVAL);
1313 To_Inet_Addr (To_In_Addr (Addr), Result);
1321 procedure Initialize (X : in out Sockets_Library_Controller) is
1322 pragma Unreferenced (X);
1332 procedure Initialize (Process_Blocking_IO : Boolean) is
1333 Expected : constant Boolean := not SOSC.Thread_Blocking_IO;
1336 if Process_Blocking_IO /= Expected then
1337 raise Socket_Error with
1338 "incorrect Process_Blocking_IO setting, expected " & Expected'Img;
1341 -- This is a dummy placeholder for an obsolete API
1343 -- Real initialization actions are in Initialize primitive operation
1344 -- of Sockets_Library_Controller.
1353 procedure Initialize is
1355 -- This is a dummy placeholder for an obsolete API
1357 -- Real initialization actions are in Initialize primitive operation
1358 -- of Sockets_Library_Controller.
1367 function Is_Empty (Item : Socket_Set_Type) return Boolean is
1369 return Item.Last = No_Socket;
1376 function Is_IP_Address (Name : String) return Boolean is
1378 for J in Name'Range loop
1380 and then Name (J) not in '0' .. '9'
1393 function Is_Open (S : Selector_Type) return Boolean is
1395 -- Either both controlling socket descriptors are valid (case of an
1396 -- open selector) or neither (case of a closed selector).
1398 pragma Assert ((S.R_Sig_Socket /= No_Socket)
1400 (S.W_Sig_Socket /= No_Socket));
1402 return S.R_Sig_Socket /= No_Socket;
1410 (Item : Socket_Set_Type;
1411 Socket : Socket_Type) return Boolean
1414 return Item.Last /= No_Socket
1415 and then Socket <= Item.Last
1416 and then Is_Socket_In_Set (Item.Set'Access, C.int (Socket)) /= 0;
1424 (First : Stream_Element_Offset;
1425 Count : C.int) return Stream_Element_Offset
1428 if First = Stream_Element_Offset'First and then Count = 0 then
1429 return Stream_Element_Offset'Last;
1431 return First + Stream_Element_Offset (Count - 1);
1439 procedure Listen_Socket
1440 (Socket : Socket_Type;
1441 Length : Natural := 15)
1443 Res : constant C.int := C_Listen (C.int (Socket), C.int (Length));
1445 if Res = Failure then
1446 Raise_Socket_Error (Socket_Errno);
1454 procedure Narrow (Item : in out Socket_Set_Type) is
1455 Last : aliased C.int := C.int (Item.Last);
1457 if Item.Last /= No_Socket then
1458 Last_Socket_In_Set (Item.Set'Access, Last'Unchecked_Access);
1459 Item.Last := Socket_Type (Last);
1463 --------------------------------
1464 -- Normalize_Empty_Socket_Set --
1465 --------------------------------
1467 procedure Normalize_Empty_Socket_Set (S : in out Socket_Set_Type) is
1469 if S.Last = No_Socket then
1470 Reset_Socket_Set (S.Set'Access);
1472 end Normalize_Empty_Socket_Set;
1478 function Official_Name (E : Host_Entry_Type) return String is
1480 return To_String (E.Official);
1487 function Official_Name (S : Service_Entry_Type) return String is
1489 return To_String (S.Official);
1492 --------------------
1493 -- Wait_On_Socket --
1494 --------------------
1496 procedure Wait_On_Socket
1497 (Socket : Socket_Type;
1499 Timeout : Selector_Duration;
1500 Selector : access Selector_Type := null;
1501 Status : out Selector_Status)
1503 type Local_Selector_Access is access Selector_Type;
1504 for Local_Selector_Access'Storage_Size use Selector_Type'Size;
1506 S : Selector_Access;
1507 -- Selector to use for waiting
1509 R_Fd_Set : Socket_Set_Type;
1510 W_Fd_Set : Socket_Set_Type;
1513 -- Create selector if not provided by the user
1515 if Selector = null then
1517 Local_S : constant Local_Selector_Access := new Selector_Type;
1519 S := Local_S.all'Unchecked_Access;
1520 Create_Selector (S.all);
1524 S := Selector.all'Access;
1528 Set (R_Fd_Set, Socket);
1530 Set (W_Fd_Set, Socket);
1533 Check_Selector (S.all, R_Fd_Set, W_Fd_Set, Status, Timeout);
1535 if Selector = null then
1536 Close_Selector (S.all);
1544 function Port_Number (S : Service_Entry_Type) return Port_Type is
1553 function Protocol_Name (S : Service_Entry_Type) return String is
1555 return To_String (S.Protocol);
1558 ----------------------
1559 -- Raise_Host_Error --
1560 ----------------------
1562 procedure Raise_Host_Error (H_Error : Integer) is
1564 raise Host_Error with
1565 Err_Code_Image (H_Error)
1566 & C.Strings.Value (Host_Error_Messages.Host_Error_Message (H_Error));
1567 end Raise_Host_Error;
1569 ------------------------
1570 -- Raise_Socket_Error --
1571 ------------------------
1573 procedure Raise_Socket_Error (Error : Integer) is
1574 use type C.Strings.chars_ptr;
1576 raise Socket_Error with
1577 Err_Code_Image (Error)
1578 & C.Strings.Value (Socket_Error_Message (Error));
1579 end Raise_Socket_Error;
1586 (Stream : in out Datagram_Socket_Stream_Type;
1587 Item : out Ada.Streams.Stream_Element_Array;
1588 Last : out Ada.Streams.Stream_Element_Offset)
1590 First : Ada.Streams.Stream_Element_Offset := Item'First;
1591 Index : Ada.Streams.Stream_Element_Offset := First - 1;
1592 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
1598 Item (First .. Max),
1604 -- Exit when all or zero data received. Zero means that the socket
1607 exit when Index < First or else Index = Max;
1618 (Stream : in out Stream_Socket_Stream_Type;
1619 Item : out Ada.Streams.Stream_Element_Array;
1620 Last : out Ada.Streams.Stream_Element_Offset)
1622 pragma Warnings (Off, Stream);
1624 First : Ada.Streams.Stream_Element_Offset := Item'First;
1625 Index : Ada.Streams.Stream_Element_Offset := First - 1;
1626 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
1630 Receive_Socket (Stream.Socket, Item (First .. Max), Index);
1633 -- Exit when all or zero data received. Zero means that the socket
1636 exit when Index < First or else Index = Max;
1642 --------------------
1643 -- Receive_Socket --
1644 --------------------
1646 procedure Receive_Socket
1647 (Socket : Socket_Type;
1648 Item : out Ada.Streams.Stream_Element_Array;
1649 Last : out Ada.Streams.Stream_Element_Offset;
1650 Flags : Request_Flag_Type := No_Request_Flag)
1656 C_Recv (C.int (Socket), Item'Address, Item'Length, To_Int (Flags));
1658 if Res = Failure then
1659 Raise_Socket_Error (Socket_Errno);
1662 Last := Last_Index (First => Item'First, Count => Res);
1665 --------------------
1666 -- Receive_Socket --
1667 --------------------
1669 procedure Receive_Socket
1670 (Socket : Socket_Type;
1671 Item : out Ada.Streams.Stream_Element_Array;
1672 Last : out Ada.Streams.Stream_Element_Offset;
1673 From : out Sock_Addr_Type;
1674 Flags : Request_Flag_Type := No_Request_Flag)
1677 Sin : aliased Sockaddr_In;
1678 Len : aliased C.int := Sin'Size / 8;
1690 if Res = Failure then
1691 Raise_Socket_Error (Socket_Errno);
1694 Last := Last_Index (First => Item'First, Count => Res);
1696 To_Inet_Addr (Sin.Sin_Addr, From.Addr);
1697 From.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
1700 --------------------
1701 -- Receive_Vector --
1702 --------------------
1704 procedure Receive_Vector
1705 (Socket : Socket_Type;
1706 Vector : Vector_Type;
1707 Count : out Ada.Streams.Stream_Element_Count;
1708 Flags : Request_Flag_Type := No_Request_Flag)
1713 (Msg_Name => System.Null_Address,
1715 Msg_Iov => Vector'Address,
1717 -- recvmsg(2) returns EMSGSIZE on Linux (and probably on other
1718 -- platforms) when the supplied vector is longer than IOV_MAX,
1719 -- so use minimum of the two lengths.
1721 Msg_Iovlen => SOSC.Msg_Iovlen_T'Min
1722 (Vector'Length, SOSC.IOV_MAX),
1724 Msg_Control => System.Null_Address,
1725 Msg_Controllen => 0,
1735 if Res = ssize_t (Failure) then
1736 Raise_Socket_Error (Socket_Errno);
1739 Count := Ada.Streams.Stream_Element_Count (Res);
1746 function Resolve_Error
1747 (Error_Value : Integer;
1748 From_Errno : Boolean := True) return Error_Type
1750 use GNAT.Sockets.SOSC;
1753 if not From_Errno then
1755 when SOSC.HOST_NOT_FOUND => return Unknown_Host;
1756 when SOSC.TRY_AGAIN => return Host_Name_Lookup_Failure;
1757 when SOSC.NO_RECOVERY => return Non_Recoverable_Error;
1758 when SOSC.NO_DATA => return Unknown_Server_Error;
1759 when others => return Cannot_Resolve_Error;
1763 -- Special case: EAGAIN may be the same value as EWOULDBLOCK, so we
1764 -- can't include it in the case statement below.
1766 pragma Warnings (Off);
1767 -- Condition "EAGAIN /= EWOULDBLOCK" is known at compile time
1769 if EAGAIN /= EWOULDBLOCK and then Error_Value = EAGAIN then
1770 return Resource_Temporarily_Unavailable;
1773 pragma Warnings (On);
1776 when ENOERROR => return Success;
1777 when EACCES => return Permission_Denied;
1778 when EADDRINUSE => return Address_Already_In_Use;
1779 when EADDRNOTAVAIL => return Cannot_Assign_Requested_Address;
1780 when EAFNOSUPPORT => return
1781 Address_Family_Not_Supported_By_Protocol;
1782 when EALREADY => return Operation_Already_In_Progress;
1783 when EBADF => return Bad_File_Descriptor;
1784 when ECONNABORTED => return Software_Caused_Connection_Abort;
1785 when ECONNREFUSED => return Connection_Refused;
1786 when ECONNRESET => return Connection_Reset_By_Peer;
1787 when EDESTADDRREQ => return Destination_Address_Required;
1788 when EFAULT => return Bad_Address;
1789 when EHOSTDOWN => return Host_Is_Down;
1790 when EHOSTUNREACH => return No_Route_To_Host;
1791 when EINPROGRESS => return Operation_Now_In_Progress;
1792 when EINTR => return Interrupted_System_Call;
1793 when EINVAL => return Invalid_Argument;
1794 when EIO => return Input_Output_Error;
1795 when EISCONN => return Transport_Endpoint_Already_Connected;
1796 when ELOOP => return Too_Many_Symbolic_Links;
1797 when EMFILE => return Too_Many_Open_Files;
1798 when EMSGSIZE => return Message_Too_Long;
1799 when ENAMETOOLONG => return File_Name_Too_Long;
1800 when ENETDOWN => return Network_Is_Down;
1801 when ENETRESET => return
1802 Network_Dropped_Connection_Because_Of_Reset;
1803 when ENETUNREACH => return Network_Is_Unreachable;
1804 when ENOBUFS => return No_Buffer_Space_Available;
1805 when ENOPROTOOPT => return Protocol_Not_Available;
1806 when ENOTCONN => return Transport_Endpoint_Not_Connected;
1807 when ENOTSOCK => return Socket_Operation_On_Non_Socket;
1808 when EOPNOTSUPP => return Operation_Not_Supported;
1809 when EPFNOSUPPORT => return Protocol_Family_Not_Supported;
1810 when EPIPE => return Broken_Pipe;
1811 when EPROTONOSUPPORT => return Protocol_Not_Supported;
1812 when EPROTOTYPE => return Protocol_Wrong_Type_For_Socket;
1813 when ESHUTDOWN => return
1814 Cannot_Send_After_Transport_Endpoint_Shutdown;
1815 when ESOCKTNOSUPPORT => return Socket_Type_Not_Supported;
1816 when ETIMEDOUT => return Connection_Timed_Out;
1817 when ETOOMANYREFS => return Too_Many_References;
1818 when EWOULDBLOCK => return Resource_Temporarily_Unavailable;
1820 when others => return Cannot_Resolve_Error;
1824 -----------------------
1825 -- Resolve_Exception --
1826 -----------------------
1828 function Resolve_Exception
1829 (Occurrence : Exception_Occurrence) return Error_Type
1831 Id : constant Exception_Id := Exception_Identity (Occurrence);
1832 Msg : constant String := Exception_Message (Occurrence);
1839 while First <= Msg'Last
1840 and then Msg (First) not in '0' .. '9'
1845 if First > Msg'Last then
1846 return Cannot_Resolve_Error;
1850 while Last < Msg'Last
1851 and then Msg (Last + 1) in '0' .. '9'
1856 Val := Integer'Value (Msg (First .. Last));
1858 if Id = Socket_Error_Id then
1859 return Resolve_Error (Val);
1861 elsif Id = Host_Error_Id then
1862 return Resolve_Error (Val, False);
1865 return Cannot_Resolve_Error;
1867 end Resolve_Exception;
1873 procedure Send_Socket
1874 (Socket : Socket_Type;
1875 Item : Ada.Streams.Stream_Element_Array;
1876 Last : out Ada.Streams.Stream_Element_Offset;
1877 Flags : Request_Flag_Type := No_Request_Flag)
1880 Send_Socket (Socket, Item, Last, To => null, Flags => Flags);
1887 procedure Send_Socket
1888 (Socket : Socket_Type;
1889 Item : Ada.Streams.Stream_Element_Array;
1890 Last : out Ada.Streams.Stream_Element_Offset;
1891 To : Sock_Addr_Type;
1892 Flags : Request_Flag_Type := No_Request_Flag)
1896 (Socket, Item, Last, To => To'Unrestricted_Access, Flags => Flags);
1903 procedure Send_Socket
1904 (Socket : Socket_Type;
1905 Item : Ada.Streams.Stream_Element_Array;
1906 Last : out Ada.Streams.Stream_Element_Offset;
1907 To : access Sock_Addr_Type;
1908 Flags : Request_Flag_Type := No_Request_Flag)
1912 Sin : aliased Sockaddr_In;
1913 C_To : System.Address;
1918 Set_Family (Sin.Sin_Family, To.Family);
1919 Set_Address (Sin'Unchecked_Access, To_In_Addr (To.Addr));
1921 (Sin'Unchecked_Access,
1922 Short_To_Network (C.unsigned_short (To.Port)));
1923 C_To := Sin'Address;
1924 Len := Sin'Size / 8;
1927 C_To := System.Null_Address;
1935 Set_Forced_Flags (To_Int (Flags)),
1939 if Res = Failure then
1940 Raise_Socket_Error (Socket_Errno);
1943 Last := Last_Index (First => Item'First, Count => Res);
1950 procedure Send_Vector
1951 (Socket : Socket_Type;
1952 Vector : Vector_Type;
1953 Count : out Ada.Streams.Stream_Element_Count;
1954 Flags : Request_Flag_Type := No_Request_Flag)
1960 Iov_Count : SOSC.Msg_Iovlen_T;
1961 This_Iov_Count : SOSC.Msg_Iovlen_T;
1967 while Iov_Count < Vector'Length loop
1969 pragma Warnings (Off);
1970 -- Following test may be compile time known on some targets
1973 (if Vector'Length - Iov_Count > SOSC.IOV_MAX
1975 else Vector'Length - Iov_Count);
1977 pragma Warnings (On);
1980 (Msg_Name => System.Null_Address,
1983 (Vector'First + Integer (Iov_Count))'Address,
1984 Msg_Iovlen => This_Iov_Count,
1985 Msg_Control => System.Null_Address,
1986 Msg_Controllen => 0,
1993 Set_Forced_Flags (To_Int (Flags)));
1995 if Res = ssize_t (Failure) then
1996 Raise_Socket_Error (Socket_Errno);
1999 Count := Count + Ada.Streams.Stream_Element_Count (Res);
2000 Iov_Count := Iov_Count + This_Iov_Count;
2008 procedure Set (Item : in out Socket_Set_Type; Socket : Socket_Type) is
2010 if Item.Last = No_Socket then
2012 -- Uninitialized socket set, make sure it is properly zeroed out
2014 Reset_Socket_Set (Item.Set'Access);
2015 Item.Last := Socket;
2017 elsif Item.Last < Socket then
2018 Item.Last := Socket;
2021 Insert_Socket_In_Set (Item.Set'Access, C.int (Socket));
2024 ----------------------
2025 -- Set_Forced_Flags --
2026 ----------------------
2028 function Set_Forced_Flags (F : C.int) return C.int is
2029 use type C.unsigned;
2030 function To_unsigned is
2031 new Ada.Unchecked_Conversion (C.int, C.unsigned);
2033 new Ada.Unchecked_Conversion (C.unsigned, C.int);
2035 return To_int (To_unsigned (F) or SOSC.MSG_Forced_Flags);
2036 end Set_Forced_Flags;
2038 -----------------------
2039 -- Set_Socket_Option --
2040 -----------------------
2042 procedure Set_Socket_Option
2043 (Socket : Socket_Type;
2044 Level : Level_Type := Socket_Level;
2045 Option : Option_Type)
2047 V8 : aliased Two_Ints;
2049 V1 : aliased C.unsigned_char;
2050 VT : aliased Timeval;
2052 Add : System.Address := Null_Address;
2061 V4 := C.int (Boolean'Pos (Option.Enabled));
2066 V8 (V8'First) := C.int (Boolean'Pos (Option.Enabled));
2067 V8 (V8'Last) := C.int (Option.Seconds);
2073 V4 := C.int (Option.Size);
2078 V4 := C.int (Boolean'Pos (True));
2082 when Add_Membership |
2084 V8 (V8'First) := To_Int (To_In_Addr (Option.Multicast_Address));
2085 V8 (V8'Last) := To_Int (To_In_Addr (Option.Local_Interface));
2089 when Multicast_If =>
2090 V4 := To_Int (To_In_Addr (Option.Outgoing_If));
2094 when Multicast_TTL =>
2095 V1 := C.unsigned_char (Option.Time_To_Live);
2099 when Multicast_Loop |
2100 Receive_Packet_Info =>
2101 V1 := C.unsigned_char (Boolean'Pos (Option.Enabled));
2107 VT := To_Timeval (Option.Timeout);
2116 Options (Option.Name),
2119 if Res = Failure then
2120 Raise_Socket_Error (Socket_Errno);
2122 end Set_Socket_Option;
2124 ----------------------
2125 -- Short_To_Network --
2126 ----------------------
2128 function Short_To_Network (S : C.unsigned_short) return C.unsigned_short is
2129 use type C.unsigned_short;
2132 -- Big-endian case. No conversion needed. On these platforms,
2133 -- htons() defaults to a null procedure.
2135 pragma Warnings (Off);
2136 -- Since the test can generate "always True/False" warning
2138 if Default_Bit_Order = High_Order_First then
2141 pragma Warnings (On);
2143 -- Little-endian case. We must swap the high and low bytes of this
2144 -- short to make the port number network compliant.
2147 return (S / 256) + (S mod 256) * 256;
2149 end Short_To_Network;
2151 ---------------------
2152 -- Shutdown_Socket --
2153 ---------------------
2155 procedure Shutdown_Socket
2156 (Socket : Socket_Type;
2157 How : Shutmode_Type := Shut_Read_Write)
2162 Res := C_Shutdown (C.int (Socket), Shutmodes (How));
2164 if Res = Failure then
2165 Raise_Socket_Error (Socket_Errno);
2167 end Shutdown_Socket;
2174 (Socket : Socket_Type;
2175 Send_To : Sock_Addr_Type) return Stream_Access
2177 S : Datagram_Socket_Stream_Access;
2180 S := new Datagram_Socket_Stream_Type;
2183 S.From := Get_Socket_Name (Socket);
2184 return Stream_Access (S);
2191 function Stream (Socket : Socket_Type) return Stream_Access is
2192 S : Stream_Socket_Stream_Access;
2194 S := new Stream_Socket_Stream_Type;
2196 return Stream_Access (S);
2203 procedure Stream_Write
2204 (Socket : Socket_Type;
2205 Item : Ada.Streams.Stream_Element_Array;
2206 To : access Sock_Addr_Type)
2208 First : Ada.Streams.Stream_Element_Offset;
2209 Index : Ada.Streams.Stream_Element_Offset;
2210 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
2213 First := Item'First;
2215 while First <= Max loop
2216 Send_Socket (Socket, Item (First .. Max), Index, To);
2218 -- Exit when all or zero data sent. Zero means that the socket has
2219 -- been closed by peer.
2221 exit when Index < First or else Index = Max;
2226 -- For an empty array, we have First > Max, and hence Index >= Max (no
2227 -- error, the loop above is never executed). After a succesful send,
2228 -- Index = Max. The only remaining case, Index < Max, is therefore
2229 -- always an actual send failure.
2232 Raise_Socket_Error (Socket_Errno);
2240 function To_C (Socket : Socket_Type) return Integer is
2242 return Integer (Socket);
2249 function To_Duration (Val : Timeval) return Timeval_Duration is
2251 return Natural (Val.Tv_Sec) * 1.0 + Natural (Val.Tv_Usec) * 1.0E-6;
2258 function To_Host_Entry (E : Hostent) return Host_Entry_Type is
2261 Official : constant String :=
2262 C.Strings.Value (E.H_Name);
2264 Aliases : constant Chars_Ptr_Array :=
2265 Chars_Ptr_Pointers.Value (E.H_Aliases);
2266 -- H_Aliases points to a list of name aliases. The list is terminated by
2269 Addresses : constant In_Addr_Access_Array :=
2270 In_Addr_Access_Pointers.Value (E.H_Addr_List);
2271 -- H_Addr_List points to a list of binary addresses (in network byte
2272 -- order). The list is terminated by a NULL pointer.
2274 -- H_Length is not used because it is currently only set to 4.
2275 -- H_Addrtype is always AF_INET
2277 Result : Host_Entry_Type
2278 (Aliases_Length => Aliases'Length - 1,
2279 Addresses_Length => Addresses'Length - 1);
2280 -- The last element is a null pointer
2286 Result.Official := To_Name (Official);
2288 Source := Aliases'First;
2289 Target := Result.Aliases'First;
2290 while Target <= Result.Aliases_Length loop
2291 Result.Aliases (Target) :=
2292 To_Name (C.Strings.Value (Aliases (Source)));
2293 Source := Source + 1;
2294 Target := Target + 1;
2297 Source := Addresses'First;
2298 Target := Result.Addresses'First;
2299 while Target <= Result.Addresses_Length loop
2300 To_Inet_Addr (Addresses (Source).all, Result.Addresses (Target));
2301 Source := Source + 1;
2302 Target := Target + 1;
2312 function To_In_Addr (Addr : Inet_Addr_Type) return In_Addr is
2314 if Addr.Family = Family_Inet then
2315 return (S_B1 => C.unsigned_char (Addr.Sin_V4 (1)),
2316 S_B2 => C.unsigned_char (Addr.Sin_V4 (2)),
2317 S_B3 => C.unsigned_char (Addr.Sin_V4 (3)),
2318 S_B4 => C.unsigned_char (Addr.Sin_V4 (4)));
2321 raise Socket_Error with "IPv6 not supported";
2328 procedure To_Inet_Addr
2330 Result : out Inet_Addr_Type) is
2332 Result.Sin_V4 (1) := Inet_Addr_Comp_Type (Addr.S_B1);
2333 Result.Sin_V4 (2) := Inet_Addr_Comp_Type (Addr.S_B2);
2334 Result.Sin_V4 (3) := Inet_Addr_Comp_Type (Addr.S_B3);
2335 Result.Sin_V4 (4) := Inet_Addr_Comp_Type (Addr.S_B4);
2342 function To_Int (F : Request_Flag_Type) return C.int
2344 Current : Request_Flag_Type := F;
2345 Result : C.int := 0;
2348 for J in Flags'Range loop
2349 exit when Current = 0;
2351 if Current mod 2 /= 0 then
2352 if Flags (J) = -1 then
2353 Raise_Socket_Error (SOSC.EOPNOTSUPP);
2356 Result := Result + Flags (J);
2359 Current := Current / 2;
2369 function To_Name (N : String) return Name_Type is
2371 return Name_Type'(N'Length, N);
2374 ----------------------
2375 -- To_Service_Entry --
2376 ----------------------
2378 function To_Service_Entry (E : Servent) return Service_Entry_Type is
2381 Official : constant String := C.Strings.Value (E.S_Name);
2383 Aliases : constant Chars_Ptr_Array :=
2384 Chars_Ptr_Pointers.Value (E.S_Aliases);
2385 -- S_Aliases points to a list of name aliases. The list is
2386 -- terminated by a NULL pointer.
2388 Protocol : constant String := C.Strings.Value (E.S_Proto);
2390 Result : Service_Entry_Type (Aliases_Length => Aliases'Length - 1);
2391 -- The last element is a null pointer
2397 Result.Official := To_Name (Official);
2399 Source := Aliases'First;
2400 Target := Result.Aliases'First;
2401 while Target <= Result.Aliases_Length loop
2402 Result.Aliases (Target) :=
2403 To_Name (C.Strings.Value (Aliases (Source)));
2404 Source := Source + 1;
2405 Target := Target + 1;
2409 Port_Type (Network_To_Short (C.unsigned_short (E.S_Port)));
2411 Result.Protocol := To_Name (Protocol);
2413 end To_Service_Entry;
2419 function To_String (HN : Name_Type) return String is
2421 return HN.Name (1 .. HN.Length);
2428 function To_Timeval (Val : Timeval_Duration) return Timeval is
2433 -- If zero, set result as zero (otherwise it gets rounded down to -1)
2439 -- Normal case where we do round down
2442 S := time_t (Val - 0.5);
2443 uS := suseconds_t (1_000_000 * (Val - Selector_Duration (S)));
2454 (Stream : in out Datagram_Socket_Stream_Type;
2455 Item : Ada.Streams.Stream_Element_Array)
2458 Stream_Write (Stream.Socket, Item, To => Stream.To'Unrestricted_Access);
2466 (Stream : in out Stream_Socket_Stream_Type;
2467 Item : Ada.Streams.Stream_Element_Array)
2470 Stream_Write (Stream.Socket, Item, To => null);
2473 Sockets_Library_Controller_Object : Sockets_Library_Controller;
2474 pragma Unreferenced (Sockets_Library_Controller_Object);
2475 -- The elaboration and finalization of this object perform the required
2476 -- initialization and cleanup actions for the sockets library.