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.
272 function "+" (L, R : Request_Flag_Type) return Request_Flag_Type is
281 procedure Abort_Selector (Selector : Selector_Type) is
285 -- Send one byte to unblock select system call
287 Res := Signalling_Fds.Write (C.int (Selector.W_Sig_Socket));
289 if Res = Failure then
290 Raise_Socket_Error (Socket_Errno);
298 procedure Accept_Socket
299 (Server : Socket_Type;
300 Socket : out Socket_Type;
301 Address : out Sock_Addr_Type)
304 Sin : aliased Sockaddr_In;
305 Len : aliased C.int := Sin'Size / 8;
308 Res := C_Accept (C.int (Server), Sin'Address, Len'Access);
310 if Res = Failure then
311 Raise_Socket_Error (Socket_Errno);
314 Socket := Socket_Type (Res);
316 To_Inet_Addr (Sin.Sin_Addr, Address.Addr);
317 Address.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
324 procedure Accept_Socket
325 (Server : Socket_Type;
326 Socket : out Socket_Type;
327 Address : out Sock_Addr_Type;
328 Timeout : Selector_Duration;
329 Selector : access Selector_Type := null;
330 Status : out Selector_Status)
333 -- Wait for socket to become available for reading
339 Selector => Selector,
342 -- Accept connection if available
344 if Status = Completed then
345 Accept_Socket (Server, Socket, Address);
356 (E : Host_Entry_Type;
357 N : Positive := 1) return Inet_Addr_Type
360 return E.Addresses (N);
363 ----------------------
364 -- Addresses_Length --
365 ----------------------
367 function Addresses_Length (E : Host_Entry_Type) return Natural is
369 return E.Addresses_Length;
370 end Addresses_Length;
377 (E : Host_Entry_Type;
378 N : Positive := 1) return String
381 return To_String (E.Aliases (N));
389 (S : Service_Entry_Type;
390 N : Positive := 1) return String
393 return To_String (S.Aliases (N));
400 function Aliases_Length (E : Host_Entry_Type) return Natural is
402 return E.Aliases_Length;
409 function Aliases_Length (S : Service_Entry_Type) return Natural is
411 return S.Aliases_Length;
418 procedure Bind_Socket
419 (Socket : Socket_Type;
420 Address : Sock_Addr_Type)
423 Sin : aliased Sockaddr_In;
424 Len : constant C.int := Sin'Size / 8;
425 -- This assumes that Address.Family = Family_Inet???
428 if Address.Family = Family_Inet6 then
429 raise Socket_Error with "IPv6 not supported";
432 Set_Family (Sin.Sin_Family, Address.Family);
433 Set_Address (Sin'Unchecked_Access, To_In_Addr (Address.Addr));
435 (Sin'Unchecked_Access,
436 Short_To_Network (C.unsigned_short (Address.Port)));
438 Res := C_Bind (C.int (Socket), Sin'Address, Len);
440 if Res = Failure then
441 Raise_Socket_Error (Socket_Errno);
449 procedure Check_Selector
450 (Selector : in out Selector_Type;
451 R_Socket_Set : in out Socket_Set_Type;
452 W_Socket_Set : in out Socket_Set_Type;
453 Status : out Selector_Status;
454 Timeout : Selector_Duration := Forever)
456 E_Socket_Set : Socket_Set_Type;
459 (Selector, R_Socket_Set, W_Socket_Set, E_Socket_Set, Status, Timeout);
466 procedure Check_Selector
467 (Selector : in out Selector_Type;
468 R_Socket_Set : in out Socket_Set_Type;
469 W_Socket_Set : in out Socket_Set_Type;
470 E_Socket_Set : in out Socket_Set_Type;
471 Status : out Selector_Status;
472 Timeout : Selector_Duration := Forever)
476 RSig : Socket_Type renames Selector.R_Sig_Socket;
477 TVal : aliased Timeval;
478 TPtr : Timeval_Access;
483 -- No timeout or Forever is indicated by a null timeval pointer
485 if Timeout = Forever then
488 TVal := To_Timeval (Timeout);
489 TPtr := TVal'Unchecked_Access;
492 -- Add read signalling socket
494 Set (R_Socket_Set, RSig);
496 Last := C.int'Max (C.int'Max (C.int (R_Socket_Set.Last),
497 C.int (W_Socket_Set.Last)),
498 C.int (E_Socket_Set.Last));
500 -- Zero out fd_set for empty Socket_Set_Type objects
502 Normalize_Empty_Socket_Set (R_Socket_Set);
503 Normalize_Empty_Socket_Set (W_Socket_Set);
504 Normalize_Empty_Socket_Set (E_Socket_Set);
509 R_Socket_Set.Set'Access,
510 W_Socket_Set.Set'Access,
511 E_Socket_Set.Set'Access,
514 if Res = Failure then
515 Raise_Socket_Error (Socket_Errno);
518 -- If Select was resumed because of read signalling socket, read this
519 -- data and remove socket from set.
521 if Is_Set (R_Socket_Set, RSig) then
522 Clear (R_Socket_Set, RSig);
524 Res := Signalling_Fds.Read (C.int (RSig));
526 if Res = Failure then
527 Raise_Socket_Error (Socket_Errno);
536 -- Update socket sets in regard to their new contents
538 Narrow (R_Socket_Set);
539 Narrow (W_Socket_Set);
540 Narrow (E_Socket_Set);
548 (Item : in out Socket_Set_Type;
549 Socket : Socket_Type)
551 Last : aliased C.int := C.int (Item.Last);
553 if Item.Last /= No_Socket then
554 Remove_Socket_From_Set (Item.Set'Access, C.int (Socket));
555 Last_Socket_In_Set (Item.Set'Access, Last'Unchecked_Access);
556 Item.Last := Socket_Type (Last);
564 procedure Close_Selector (Selector : in out Selector_Type) is
566 -- Close the signalling file descriptors used internally for the
567 -- implementation of Abort_Selector.
569 Signalling_Fds.Close (C.int (Selector.R_Sig_Socket));
570 Signalling_Fds.Close (C.int (Selector.W_Sig_Socket));
572 -- Reset R_Sig_Socket and W_Sig_Socket to No_Socket to ensure that any
573 -- (erroneous) subsequent attempt to use this selector properly fails.
575 Selector.R_Sig_Socket := No_Socket;
576 Selector.W_Sig_Socket := No_Socket;
583 procedure Close_Socket (Socket : Socket_Type) is
587 Res := C_Close (C.int (Socket));
589 if Res = Failure then
590 Raise_Socket_Error (Socket_Errno);
598 procedure Connect_Socket
599 (Socket : Socket_Type;
600 Server : Sock_Addr_Type)
603 Sin : aliased Sockaddr_In;
604 Len : constant C.int := Sin'Size / 8;
607 if Server.Family = Family_Inet6 then
608 raise Socket_Error with "IPv6 not supported";
611 Set_Family (Sin.Sin_Family, Server.Family);
612 Set_Address (Sin'Unchecked_Access, To_In_Addr (Server.Addr));
614 (Sin'Unchecked_Access,
615 Short_To_Network (C.unsigned_short (Server.Port)));
617 Res := C_Connect (C.int (Socket), Sin'Address, Len);
619 if Res = Failure then
620 Raise_Socket_Error (Socket_Errno);
628 procedure Connect_Socket
629 (Socket : Socket_Type;
630 Server : Sock_Addr_Type;
631 Timeout : Selector_Duration;
632 Selector : access Selector_Type := null;
633 Status : out Selector_Status)
636 -- Used to set Socket to non-blocking I/O
639 -- Set the socket to non-blocking I/O
641 Req := (Name => Non_Blocking_IO, Enabled => True);
642 Control_Socket (Socket, Request => Req);
644 -- Start operation (non-blocking), will raise Socket_Error with
648 Connect_Socket (Socket, Server);
650 when E : Socket_Error =>
651 if Resolve_Exception (E) = Operation_Now_In_Progress then
658 -- Wait for socket to become available for writing
664 Selector => Selector,
667 -- Reset the socket to blocking I/O
669 Req := (Name => Non_Blocking_IO, Enabled => False);
670 Control_Socket (Socket, Request => Req);
677 procedure Control_Socket
678 (Socket : Socket_Type;
679 Request : in out Request_Type)
686 when Non_Blocking_IO =>
687 Arg := C.int (Boolean'Pos (Request.Enabled));
689 when N_Bytes_To_Read =>
694 (C.int (Socket), Requests (Request.Name), Arg'Unchecked_Access);
696 if Res = Failure then
697 Raise_Socket_Error (Socket_Errno);
701 when Non_Blocking_IO =>
704 when N_Bytes_To_Read =>
705 Request.Size := Natural (Arg);
714 (Source : Socket_Set_Type;
715 Target : out Socket_Set_Type)
721 ---------------------
722 -- Create_Selector --
723 ---------------------
725 procedure Create_Selector (Selector : out Selector_Type) is
726 Two_Fds : aliased Fd_Pair;
730 -- We open two signalling file descriptors. One of them is used to send
731 -- data to the other, which is included in a C_Select socket set. The
732 -- communication is used to force a call to C_Select to complete, and
733 -- the waiting task to resume its execution.
735 Res := Signalling_Fds.Create (Two_Fds'Access);
737 if Res = Failure then
738 Raise_Socket_Error (Socket_Errno);
741 Selector.R_Sig_Socket := Socket_Type (Two_Fds (Read_End));
742 Selector.W_Sig_Socket := Socket_Type (Two_Fds (Write_End));
749 procedure Create_Socket
750 (Socket : out Socket_Type;
751 Family : Family_Type := Family_Inet;
752 Mode : Mode_Type := Socket_Stream)
757 Res := C_Socket (Families (Family), Modes (Mode), 0);
759 if Res = Failure then
760 Raise_Socket_Error (Socket_Errno);
763 Socket := Socket_Type (Res);
770 procedure Empty (Item : out Socket_Set_Type) is
772 Reset_Socket_Set (Item.Set'Access);
773 Item.Last := No_Socket;
780 function Err_Code_Image (E : Integer) return String is
781 Msg : String := E'Img & "] ";
783 Msg (Msg'First) := '[';
791 procedure Finalize (X : in out Sockets_Library_Controller) is
792 pragma Unreferenced (X);
795 -- Finalization operation for the GNAT.Sockets package
804 procedure Finalize is
806 -- This is a dummy placeholder for an obsolete API.
807 -- The real finalization actions are in Initialize primitive operation
808 -- of Sockets_Library_Controller.
818 (Item : in out Socket_Set_Type;
819 Socket : out Socket_Type)
822 L : aliased C.int := C.int (Item.Last);
825 if Item.Last /= No_Socket then
827 (Item.Set'Access, Last => L'Access, Socket => S'Access);
828 Item.Last := Socket_Type (L);
829 Socket := Socket_Type (S);
840 (Stream : not null Stream_Access) return Sock_Addr_Type
843 if Stream.all in Datagram_Socket_Stream_Type then
844 return Datagram_Socket_Stream_Type (Stream.all).From;
846 return Get_Peer_Name (Stream_Socket_Stream_Type (Stream.all).Socket);
850 -------------------------
851 -- Get_Host_By_Address --
852 -------------------------
854 function Get_Host_By_Address
855 (Address : Inet_Addr_Type;
856 Family : Family_Type := Family_Inet) return Host_Entry_Type
858 pragma Unreferenced (Family);
860 HA : aliased In_Addr := To_In_Addr (Address);
861 Buflen : constant C.int := Netdb_Buffer_Size;
862 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
863 Res : aliased Hostent;
867 if Safe_Gethostbyaddr (HA'Address, HA'Size / 8, SOSC.AF_INET,
868 Res'Access, Buf'Address, Buflen, Err'Access) /= 0
870 Raise_Host_Error (Integer (Err));
873 return To_Host_Entry (Res);
874 end Get_Host_By_Address;
876 ----------------------
877 -- Get_Host_By_Name --
878 ----------------------
880 function Get_Host_By_Name (Name : String) return Host_Entry_Type is
882 -- Detect IP address name and redirect to Inet_Addr
884 if Is_IP_Address (Name) then
885 return Get_Host_By_Address (Inet_Addr (Name));
889 HN : constant C.char_array := C.To_C (Name);
890 Buflen : constant C.int := Netdb_Buffer_Size;
891 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
892 Res : aliased Hostent;
896 if Safe_Gethostbyname
897 (HN, Res'Access, Buf'Address, Buflen, Err'Access) /= 0
899 Raise_Host_Error (Integer (Err));
902 return To_Host_Entry (Res);
904 end Get_Host_By_Name;
910 function Get_Peer_Name (Socket : Socket_Type) return Sock_Addr_Type is
911 Sin : aliased Sockaddr_In;
912 Len : aliased C.int := Sin'Size / 8;
913 Res : Sock_Addr_Type (Family_Inet);
916 if C_Getpeername (C.int (Socket), Sin'Address, Len'Access) = Failure then
917 Raise_Socket_Error (Socket_Errno);
920 To_Inet_Addr (Sin.Sin_Addr, Res.Addr);
921 Res.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
926 -------------------------
927 -- Get_Service_By_Name --
928 -------------------------
930 function Get_Service_By_Name
932 Protocol : String) return Service_Entry_Type
934 SN : constant C.char_array := C.To_C (Name);
935 SP : constant C.char_array := C.To_C (Protocol);
936 Buflen : constant C.int := Netdb_Buffer_Size;
937 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
938 Res : aliased Servent;
941 if Safe_Getservbyname (SN, SP, Res'Access, Buf'Address, Buflen) /= 0 then
942 raise Service_Error with "Service not found";
945 -- Translate from the C format to the API format
947 return To_Service_Entry (Res);
948 end Get_Service_By_Name;
950 -------------------------
951 -- Get_Service_By_Port --
952 -------------------------
954 function Get_Service_By_Port
956 Protocol : String) return Service_Entry_Type
958 SP : constant C.char_array := C.To_C (Protocol);
959 Buflen : constant C.int := Netdb_Buffer_Size;
960 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
961 Res : aliased Servent;
964 if Safe_Getservbyport
965 (C.int (Short_To_Network (C.unsigned_short (Port))), SP,
966 Res'Access, Buf'Address, Buflen) /= 0
968 raise Service_Error with "Service not found";
971 -- Translate from the C format to the API format
973 return To_Service_Entry (Res);
974 end Get_Service_By_Port;
976 ---------------------
977 -- Get_Socket_Name --
978 ---------------------
980 function Get_Socket_Name
981 (Socket : Socket_Type) return Sock_Addr_Type
983 Sin : aliased Sockaddr_In;
984 Len : aliased C.int := Sin'Size / 8;
986 Addr : Sock_Addr_Type := No_Sock_Addr;
989 Res := C_Getsockname (C.int (Socket), Sin'Address, Len'Access);
991 if Res /= Failure then
992 To_Inet_Addr (Sin.Sin_Addr, Addr.Addr);
993 Addr.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
999 -----------------------
1000 -- Get_Socket_Option --
1001 -----------------------
1003 function Get_Socket_Option
1004 (Socket : Socket_Type;
1005 Level : Level_Type := Socket_Level;
1006 Name : Option_Name) return Option_Type
1008 use type C.unsigned_char;
1010 V8 : aliased Two_Ints;
1012 V1 : aliased C.unsigned_char;
1013 VT : aliased Timeval;
1014 Len : aliased C.int;
1015 Add : System.Address;
1017 Opt : Option_Type (Name);
1021 when Multicast_Loop |
1023 Receive_Packet_Info =>
1058 if Res = Failure then
1059 Raise_Socket_Error (Socket_Errno);
1067 Opt.Enabled := (V4 /= 0);
1070 Opt.Enabled := (V8 (V8'First) /= 0);
1071 Opt.Seconds := Natural (V8 (V8'Last));
1075 Opt.Size := Natural (V4);
1078 Opt.Error := Resolve_Error (Integer (V4));
1080 when Add_Membership |
1082 To_Inet_Addr (To_In_Addr (V8 (V8'First)), Opt.Multicast_Address);
1083 To_Inet_Addr (To_In_Addr (V8 (V8'Last)), Opt.Local_Interface);
1085 when Multicast_If =>
1086 To_Inet_Addr (To_In_Addr (V4), Opt.Outgoing_If);
1088 when Multicast_TTL =>
1089 Opt.Time_To_Live := Integer (V1);
1091 when Multicast_Loop |
1092 Receive_Packet_Info =>
1093 Opt.Enabled := (V1 /= 0);
1097 Opt.Timeout := To_Duration (VT);
1101 end Get_Socket_Option;
1107 function Host_Name return String is
1108 Name : aliased C.char_array (1 .. 64);
1112 Res := C_Gethostname (Name'Address, Name'Length);
1114 if Res = Failure then
1115 Raise_Socket_Error (Socket_Errno);
1118 return C.To_Ada (Name);
1126 (Val : Inet_Addr_VN_Type;
1127 Hex : Boolean := False) return String
1129 -- The largest Inet_Addr_Comp_Type image occurs with IPv4. It
1130 -- has at most a length of 3 plus one '.' character.
1132 Buffer : String (1 .. 4 * Val'Length);
1133 Length : Natural := 1;
1134 Separator : Character;
1136 procedure Img10 (V : Inet_Addr_Comp_Type);
1137 -- Append to Buffer image of V in decimal format
1139 procedure Img16 (V : Inet_Addr_Comp_Type);
1140 -- Append to Buffer image of V in hexadecimal format
1146 procedure Img10 (V : Inet_Addr_Comp_Type) is
1147 Img : constant String := V'Img;
1148 Len : constant Natural := Img'Length - 1;
1150 Buffer (Length .. Length + Len - 1) := Img (2 .. Img'Last);
1151 Length := Length + Len;
1158 procedure Img16 (V : Inet_Addr_Comp_Type) is
1160 Buffer (Length) := Hex_To_Char (Natural (V / 16) + 1);
1161 Buffer (Length + 1) := Hex_To_Char (Natural (V mod 16) + 1);
1162 Length := Length + 2;
1165 -- Start of processing for Image
1168 Separator := (if Hex then ':' else '.');
1170 for J in Val'Range loop
1177 if J /= Val'Last then
1178 Buffer (Length) := Separator;
1179 Length := Length + 1;
1183 return Buffer (1 .. Length - 1);
1190 function Image (Value : Inet_Addr_Type) return String is
1192 if Value.Family = Family_Inet then
1193 return Image (Inet_Addr_VN_Type (Value.Sin_V4), Hex => False);
1195 return Image (Inet_Addr_VN_Type (Value.Sin_V6), Hex => True);
1203 function Image (Value : Sock_Addr_Type) return String is
1204 Port : constant String := Value.Port'Img;
1206 return Image (Value.Addr) & ':' & Port (2 .. Port'Last);
1213 function Image (Socket : Socket_Type) return String is
1222 function Image (Item : Socket_Set_Type) return String is
1223 Socket_Set : Socket_Set_Type := Item;
1227 Last_Img : constant String := Socket_Set.Last'Img;
1229 (1 .. (Integer (Socket_Set.Last) + 1) * Last_Img'Length);
1230 Index : Positive := 1;
1231 Socket : Socket_Type;
1234 while not Is_Empty (Socket_Set) loop
1235 Get (Socket_Set, Socket);
1238 Socket_Img : constant String := Socket'Img;
1240 Buffer (Index .. Index + Socket_Img'Length - 1) := Socket_Img;
1241 Index := Index + Socket_Img'Length;
1245 return "[" & Last_Img & "]" & Buffer (1 .. Index - 1);
1253 function Inet_Addr (Image : String) return Inet_Addr_Type is
1255 use Interfaces.C.Strings;
1257 Img : aliased char_array := To_C (Image);
1258 Cp : constant chars_ptr := To_Chars_Ptr (Img'Unchecked_Access);
1259 Addr : aliased C.int;
1261 Result : Inet_Addr_Type;
1264 -- Special case for an empty Image as on some platforms (e.g. Windows)
1265 -- calling Inet_Addr("") will not return an error.
1268 Raise_Socket_Error (SOSC.EINVAL);
1271 Res := Inet_Pton (SOSC.AF_INET, Cp, Addr'Address);
1274 Raise_Socket_Error (Socket_Errno);
1277 Raise_Socket_Error (SOSC.EINVAL);
1280 To_Inet_Addr (To_In_Addr (Addr), Result);
1288 procedure Initialize (X : in out Sockets_Library_Controller) is
1289 pragma Unreferenced (X);
1299 procedure Initialize (Process_Blocking_IO : Boolean) is
1300 Expected : constant Boolean := not SOSC.Thread_Blocking_IO;
1303 if Process_Blocking_IO /= Expected then
1304 raise Socket_Error with
1305 "incorrect Process_Blocking_IO setting, expected " & Expected'Img;
1308 -- This is a dummy placeholder for an obsolete API
1310 -- Real initialization actions are in Initialize primitive operation
1311 -- of Sockets_Library_Controller.
1320 procedure Initialize is
1322 -- This is a dummy placeholder for an obsolete API
1324 -- Real initialization actions are in Initialize primitive operation
1325 -- of Sockets_Library_Controller.
1334 function Is_Empty (Item : Socket_Set_Type) return Boolean is
1336 return Item.Last = No_Socket;
1343 function Is_IP_Address (Name : String) return Boolean is
1345 for J in Name'Range loop
1347 and then Name (J) not in '0' .. '9'
1361 (Item : Socket_Set_Type;
1362 Socket : Socket_Type) return Boolean
1365 return Item.Last /= No_Socket
1366 and then Socket <= Item.Last
1367 and then Is_Socket_In_Set (Item.Set'Access, C.int (Socket)) /= 0;
1375 (First : Stream_Element_Offset;
1376 Count : C.int) return Stream_Element_Offset
1379 if First = Stream_Element_Offset'First and then Count = 0 then
1380 return Stream_Element_Offset'Last;
1382 return First + Stream_Element_Offset (Count - 1);
1390 procedure Listen_Socket
1391 (Socket : Socket_Type;
1392 Length : Natural := 15)
1394 Res : constant C.int := C_Listen (C.int (Socket), C.int (Length));
1396 if Res = Failure then
1397 Raise_Socket_Error (Socket_Errno);
1405 procedure Narrow (Item : in out Socket_Set_Type) is
1406 Last : aliased C.int := C.int (Item.Last);
1408 if Item.Last /= No_Socket then
1409 Last_Socket_In_Set (Item.Set'Access, Last'Unchecked_Access);
1410 Item.Last := Socket_Type (Last);
1414 --------------------------------
1415 -- Normalize_Empty_Socket_Set --
1416 --------------------------------
1418 procedure Normalize_Empty_Socket_Set (S : in out Socket_Set_Type) is
1420 if S.Last = No_Socket then
1421 Reset_Socket_Set (S.Set'Access);
1423 end Normalize_Empty_Socket_Set;
1429 function Official_Name (E : Host_Entry_Type) return String is
1431 return To_String (E.Official);
1438 function Official_Name (S : Service_Entry_Type) return String is
1440 return To_String (S.Official);
1443 --------------------
1444 -- Wait_On_Socket --
1445 --------------------
1447 procedure Wait_On_Socket
1448 (Socket : Socket_Type;
1450 Timeout : Selector_Duration;
1451 Selector : access Selector_Type := null;
1452 Status : out Selector_Status)
1454 type Local_Selector_Access is access Selector_Type;
1455 for Local_Selector_Access'Storage_Size use Selector_Type'Size;
1457 S : Selector_Access;
1458 -- Selector to use for waiting
1460 R_Fd_Set : Socket_Set_Type;
1461 W_Fd_Set : Socket_Set_Type;
1464 -- Create selector if not provided by the user
1466 if Selector = null then
1468 Local_S : constant Local_Selector_Access := new Selector_Type;
1470 S := Local_S.all'Unchecked_Access;
1471 Create_Selector (S.all);
1475 S := Selector.all'Access;
1479 Set (R_Fd_Set, Socket);
1481 Set (W_Fd_Set, Socket);
1484 Check_Selector (S.all, R_Fd_Set, W_Fd_Set, Status, Timeout);
1486 if Selector = null then
1487 Close_Selector (S.all);
1495 function Port_Number (S : Service_Entry_Type) return Port_Type is
1504 function Protocol_Name (S : Service_Entry_Type) return String is
1506 return To_String (S.Protocol);
1509 ----------------------
1510 -- Raise_Host_Error --
1511 ----------------------
1513 procedure Raise_Host_Error (H_Error : Integer) is
1515 raise Host_Error with
1516 Err_Code_Image (H_Error)
1517 & C.Strings.Value (Host_Error_Messages.Host_Error_Message (H_Error));
1518 end Raise_Host_Error;
1520 ------------------------
1521 -- Raise_Socket_Error --
1522 ------------------------
1524 procedure Raise_Socket_Error (Error : Integer) is
1525 use type C.Strings.chars_ptr;
1527 raise Socket_Error with
1528 Err_Code_Image (Error)
1529 & C.Strings.Value (Socket_Error_Message (Error));
1530 end Raise_Socket_Error;
1537 (Stream : in out Datagram_Socket_Stream_Type;
1538 Item : out Ada.Streams.Stream_Element_Array;
1539 Last : out Ada.Streams.Stream_Element_Offset)
1541 First : Ada.Streams.Stream_Element_Offset := Item'First;
1542 Index : Ada.Streams.Stream_Element_Offset := First - 1;
1543 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
1549 Item (First .. Max),
1555 -- Exit when all or zero data received. Zero means that the socket
1558 exit when Index < First or else Index = Max;
1569 (Stream : in out Stream_Socket_Stream_Type;
1570 Item : out Ada.Streams.Stream_Element_Array;
1571 Last : out Ada.Streams.Stream_Element_Offset)
1573 pragma Warnings (Off, Stream);
1575 First : Ada.Streams.Stream_Element_Offset := Item'First;
1576 Index : Ada.Streams.Stream_Element_Offset := First - 1;
1577 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
1581 Receive_Socket (Stream.Socket, Item (First .. Max), Index);
1584 -- Exit when all or zero data received. Zero means that the socket
1587 exit when Index < First or else Index = Max;
1593 --------------------
1594 -- Receive_Socket --
1595 --------------------
1597 procedure Receive_Socket
1598 (Socket : Socket_Type;
1599 Item : out Ada.Streams.Stream_Element_Array;
1600 Last : out Ada.Streams.Stream_Element_Offset;
1601 Flags : Request_Flag_Type := No_Request_Flag)
1607 C_Recv (C.int (Socket), Item'Address, Item'Length, To_Int (Flags));
1609 if Res = Failure then
1610 Raise_Socket_Error (Socket_Errno);
1613 Last := Last_Index (First => Item'First, Count => Res);
1616 --------------------
1617 -- Receive_Socket --
1618 --------------------
1620 procedure Receive_Socket
1621 (Socket : Socket_Type;
1622 Item : out Ada.Streams.Stream_Element_Array;
1623 Last : out Ada.Streams.Stream_Element_Offset;
1624 From : out Sock_Addr_Type;
1625 Flags : Request_Flag_Type := No_Request_Flag)
1628 Sin : aliased Sockaddr_In;
1629 Len : aliased C.int := Sin'Size / 8;
1641 if Res = Failure then
1642 Raise_Socket_Error (Socket_Errno);
1645 Last := Last_Index (First => Item'First, Count => Res);
1647 To_Inet_Addr (Sin.Sin_Addr, From.Addr);
1648 From.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
1651 --------------------
1652 -- Receive_Vector --
1653 --------------------
1655 procedure Receive_Vector
1656 (Socket : Socket_Type;
1657 Vector : Vector_Type;
1658 Count : out Ada.Streams.Stream_Element_Count;
1659 Flags : Request_Flag_Type := No_Request_Flag)
1664 (Msg_Name => System.Null_Address,
1666 Msg_Iov => Vector'Address,
1668 -- recvmsg(2) returns EMSGSIZE on Linux (and probably on other
1669 -- platforms) when the supplied vector is longer than IOV_MAX,
1670 -- so use minimum of the two lengths.
1672 Msg_Iovlen => SOSC.Msg_Iovlen_T'Min
1673 (Vector'Length, SOSC.IOV_MAX),
1675 Msg_Control => System.Null_Address,
1676 Msg_Controllen => 0,
1686 if Res = ssize_t (Failure) then
1687 Raise_Socket_Error (Socket_Errno);
1690 Count := Ada.Streams.Stream_Element_Count (Res);
1697 function Resolve_Error
1698 (Error_Value : Integer;
1699 From_Errno : Boolean := True) return Error_Type
1701 use GNAT.Sockets.SOSC;
1704 if not From_Errno then
1706 when SOSC.HOST_NOT_FOUND => return Unknown_Host;
1707 when SOSC.TRY_AGAIN => return Host_Name_Lookup_Failure;
1708 when SOSC.NO_RECOVERY => return Non_Recoverable_Error;
1709 when SOSC.NO_DATA => return Unknown_Server_Error;
1710 when others => return Cannot_Resolve_Error;
1714 -- Special case: EAGAIN may be the same value as EWOULDBLOCK, so we
1715 -- can't include it in the case statement below.
1717 pragma Warnings (Off);
1718 -- Condition "EAGAIN /= EWOULDBLOCK" is known at compile time
1720 if EAGAIN /= EWOULDBLOCK and then Error_Value = EAGAIN then
1721 return Resource_Temporarily_Unavailable;
1724 pragma Warnings (On);
1727 when ENOERROR => return Success;
1728 when EACCES => return Permission_Denied;
1729 when EADDRINUSE => return Address_Already_In_Use;
1730 when EADDRNOTAVAIL => return Cannot_Assign_Requested_Address;
1731 when EAFNOSUPPORT => return
1732 Address_Family_Not_Supported_By_Protocol;
1733 when EALREADY => return Operation_Already_In_Progress;
1734 when EBADF => return Bad_File_Descriptor;
1735 when ECONNABORTED => return Software_Caused_Connection_Abort;
1736 when ECONNREFUSED => return Connection_Refused;
1737 when ECONNRESET => return Connection_Reset_By_Peer;
1738 when EDESTADDRREQ => return Destination_Address_Required;
1739 when EFAULT => return Bad_Address;
1740 when EHOSTDOWN => return Host_Is_Down;
1741 when EHOSTUNREACH => return No_Route_To_Host;
1742 when EINPROGRESS => return Operation_Now_In_Progress;
1743 when EINTR => return Interrupted_System_Call;
1744 when EINVAL => return Invalid_Argument;
1745 when EIO => return Input_Output_Error;
1746 when EISCONN => return Transport_Endpoint_Already_Connected;
1747 when ELOOP => return Too_Many_Symbolic_Links;
1748 when EMFILE => return Too_Many_Open_Files;
1749 when EMSGSIZE => return Message_Too_Long;
1750 when ENAMETOOLONG => return File_Name_Too_Long;
1751 when ENETDOWN => return Network_Is_Down;
1752 when ENETRESET => return
1753 Network_Dropped_Connection_Because_Of_Reset;
1754 when ENETUNREACH => return Network_Is_Unreachable;
1755 when ENOBUFS => return No_Buffer_Space_Available;
1756 when ENOPROTOOPT => return Protocol_Not_Available;
1757 when ENOTCONN => return Transport_Endpoint_Not_Connected;
1758 when ENOTSOCK => return Socket_Operation_On_Non_Socket;
1759 when EOPNOTSUPP => return Operation_Not_Supported;
1760 when EPFNOSUPPORT => return Protocol_Family_Not_Supported;
1761 when EPIPE => return Broken_Pipe;
1762 when EPROTONOSUPPORT => return Protocol_Not_Supported;
1763 when EPROTOTYPE => return Protocol_Wrong_Type_For_Socket;
1764 when ESHUTDOWN => return
1765 Cannot_Send_After_Transport_Endpoint_Shutdown;
1766 when ESOCKTNOSUPPORT => return Socket_Type_Not_Supported;
1767 when ETIMEDOUT => return Connection_Timed_Out;
1768 when ETOOMANYREFS => return Too_Many_References;
1769 when EWOULDBLOCK => return Resource_Temporarily_Unavailable;
1771 when others => return Cannot_Resolve_Error;
1775 -----------------------
1776 -- Resolve_Exception --
1777 -----------------------
1779 function Resolve_Exception
1780 (Occurrence : Exception_Occurrence) return Error_Type
1782 Id : constant Exception_Id := Exception_Identity (Occurrence);
1783 Msg : constant String := Exception_Message (Occurrence);
1790 while First <= Msg'Last
1791 and then Msg (First) not in '0' .. '9'
1796 if First > Msg'Last then
1797 return Cannot_Resolve_Error;
1801 while Last < Msg'Last
1802 and then Msg (Last + 1) in '0' .. '9'
1807 Val := Integer'Value (Msg (First .. Last));
1809 if Id = Socket_Error_Id then
1810 return Resolve_Error (Val);
1812 elsif Id = Host_Error_Id then
1813 return Resolve_Error (Val, False);
1816 return Cannot_Resolve_Error;
1818 end Resolve_Exception;
1824 procedure Send_Socket
1825 (Socket : Socket_Type;
1826 Item : Ada.Streams.Stream_Element_Array;
1827 Last : out Ada.Streams.Stream_Element_Offset;
1828 Flags : Request_Flag_Type := No_Request_Flag)
1831 Send_Socket (Socket, Item, Last, To => null, Flags => Flags);
1838 procedure Send_Socket
1839 (Socket : Socket_Type;
1840 Item : Ada.Streams.Stream_Element_Array;
1841 Last : out Ada.Streams.Stream_Element_Offset;
1842 To : Sock_Addr_Type;
1843 Flags : Request_Flag_Type := No_Request_Flag)
1847 (Socket, Item, Last, To => To'Unrestricted_Access, Flags => Flags);
1854 procedure Send_Socket
1855 (Socket : Socket_Type;
1856 Item : Ada.Streams.Stream_Element_Array;
1857 Last : out Ada.Streams.Stream_Element_Offset;
1858 To : access Sock_Addr_Type;
1859 Flags : Request_Flag_Type := No_Request_Flag)
1863 Sin : aliased Sockaddr_In;
1864 C_To : System.Address;
1869 Set_Family (Sin.Sin_Family, To.Family);
1870 Set_Address (Sin'Unchecked_Access, To_In_Addr (To.Addr));
1872 (Sin'Unchecked_Access,
1873 Short_To_Network (C.unsigned_short (To.Port)));
1874 C_To := Sin'Address;
1875 Len := Sin'Size / 8;
1878 C_To := System.Null_Address;
1886 Set_Forced_Flags (To_Int (Flags)),
1890 if Res = Failure then
1891 Raise_Socket_Error (Socket_Errno);
1894 Last := Last_Index (First => Item'First, Count => Res);
1901 procedure Send_Vector
1902 (Socket : Socket_Type;
1903 Vector : Vector_Type;
1904 Count : out Ada.Streams.Stream_Element_Count;
1905 Flags : Request_Flag_Type := No_Request_Flag)
1911 Iov_Count : SOSC.Msg_Iovlen_T;
1912 This_Iov_Count : SOSC.Msg_Iovlen_T;
1918 while Iov_Count < Vector'Length loop
1920 pragma Warnings (Off);
1921 -- Following test may be compile time known on some targets
1924 (if Vector'Length - Iov_Count > SOSC.IOV_MAX
1926 else Vector'Length - Iov_Count);
1928 pragma Warnings (On);
1931 (Msg_Name => System.Null_Address,
1934 (Vector'First + Integer (Iov_Count))'Address,
1935 Msg_Iovlen => This_Iov_Count,
1936 Msg_Control => System.Null_Address,
1937 Msg_Controllen => 0,
1944 Set_Forced_Flags (To_Int (Flags)));
1946 if Res = ssize_t (Failure) then
1947 Raise_Socket_Error (Socket_Errno);
1950 Count := Count + Ada.Streams.Stream_Element_Count (Res);
1951 Iov_Count := Iov_Count + This_Iov_Count;
1959 procedure Set (Item : in out Socket_Set_Type; Socket : Socket_Type) is
1961 if Item.Last = No_Socket then
1963 -- Uninitialized socket set, make sure it is properly zeroed out
1965 Reset_Socket_Set (Item.Set'Access);
1966 Item.Last := Socket;
1968 elsif Item.Last < Socket then
1969 Item.Last := Socket;
1972 Insert_Socket_In_Set (Item.Set'Access, C.int (Socket));
1975 ----------------------
1976 -- Set_Forced_Flags --
1977 ----------------------
1979 function Set_Forced_Flags (F : C.int) return C.int is
1980 use type C.unsigned;
1981 function To_unsigned is
1982 new Ada.Unchecked_Conversion (C.int, C.unsigned);
1984 new Ada.Unchecked_Conversion (C.unsigned, C.int);
1986 return To_int (To_unsigned (F) or SOSC.MSG_Forced_Flags);
1987 end Set_Forced_Flags;
1989 -----------------------
1990 -- Set_Socket_Option --
1991 -----------------------
1993 procedure Set_Socket_Option
1994 (Socket : Socket_Type;
1995 Level : Level_Type := Socket_Level;
1996 Option : Option_Type)
1998 V8 : aliased Two_Ints;
2000 V1 : aliased C.unsigned_char;
2001 VT : aliased Timeval;
2003 Add : System.Address := Null_Address;
2012 V4 := C.int (Boolean'Pos (Option.Enabled));
2017 V8 (V8'First) := C.int (Boolean'Pos (Option.Enabled));
2018 V8 (V8'Last) := C.int (Option.Seconds);
2024 V4 := C.int (Option.Size);
2029 V4 := C.int (Boolean'Pos (True));
2033 when Add_Membership |
2035 V8 (V8'First) := To_Int (To_In_Addr (Option.Multicast_Address));
2036 V8 (V8'Last) := To_Int (To_In_Addr (Option.Local_Interface));
2040 when Multicast_If =>
2041 V4 := To_Int (To_In_Addr (Option.Outgoing_If));
2045 when Multicast_TTL =>
2046 V1 := C.unsigned_char (Option.Time_To_Live);
2050 when Multicast_Loop |
2051 Receive_Packet_Info =>
2052 V1 := C.unsigned_char (Boolean'Pos (Option.Enabled));
2058 VT := To_Timeval (Option.Timeout);
2067 Options (Option.Name),
2070 if Res = Failure then
2071 Raise_Socket_Error (Socket_Errno);
2073 end Set_Socket_Option;
2075 ----------------------
2076 -- Short_To_Network --
2077 ----------------------
2079 function Short_To_Network (S : C.unsigned_short) return C.unsigned_short is
2080 use type C.unsigned_short;
2083 -- Big-endian case. No conversion needed. On these platforms,
2084 -- htons() defaults to a null procedure.
2086 pragma Warnings (Off);
2087 -- Since the test can generate "always True/False" warning
2089 if Default_Bit_Order = High_Order_First then
2092 pragma Warnings (On);
2094 -- Little-endian case. We must swap the high and low bytes of this
2095 -- short to make the port number network compliant.
2098 return (S / 256) + (S mod 256) * 256;
2100 end Short_To_Network;
2102 ---------------------
2103 -- Shutdown_Socket --
2104 ---------------------
2106 procedure Shutdown_Socket
2107 (Socket : Socket_Type;
2108 How : Shutmode_Type := Shut_Read_Write)
2113 Res := C_Shutdown (C.int (Socket), Shutmodes (How));
2115 if Res = Failure then
2116 Raise_Socket_Error (Socket_Errno);
2118 end Shutdown_Socket;
2125 (Socket : Socket_Type;
2126 Send_To : Sock_Addr_Type) return Stream_Access
2128 S : Datagram_Socket_Stream_Access;
2131 S := new Datagram_Socket_Stream_Type;
2134 S.From := Get_Socket_Name (Socket);
2135 return Stream_Access (S);
2142 function Stream (Socket : Socket_Type) return Stream_Access is
2143 S : Stream_Socket_Stream_Access;
2145 S := new Stream_Socket_Stream_Type;
2147 return Stream_Access (S);
2154 procedure Stream_Write
2155 (Socket : Socket_Type;
2156 Item : Ada.Streams.Stream_Element_Array;
2157 To : access Sock_Addr_Type)
2159 First : Ada.Streams.Stream_Element_Offset;
2160 Index : Ada.Streams.Stream_Element_Offset;
2161 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
2164 First := Item'First;
2166 while First <= Max loop
2167 Send_Socket (Socket, Item (First .. Max), Index, To);
2169 -- Exit when all or zero data sent. Zero means that the socket has
2170 -- been closed by peer.
2172 exit when Index < First or else Index = Max;
2177 -- For an empty array, we have First > Max, and hence Index >= Max (no
2178 -- error, the loop above is never executed). After a succesful send,
2179 -- Index = Max. The only remaining case, Index < Max, is therefore
2180 -- always an actual send failure.
2183 Raise_Socket_Error (Socket_Errno);
2191 function To_C (Socket : Socket_Type) return Integer is
2193 return Integer (Socket);
2200 function To_Duration (Val : Timeval) return Timeval_Duration is
2202 return Natural (Val.Tv_Sec) * 1.0 + Natural (Val.Tv_Usec) * 1.0E-6;
2209 function To_Host_Entry (E : Hostent) return Host_Entry_Type is
2212 Official : constant String :=
2213 C.Strings.Value (E.H_Name);
2215 Aliases : constant Chars_Ptr_Array :=
2216 Chars_Ptr_Pointers.Value (E.H_Aliases);
2217 -- H_Aliases points to a list of name aliases. The list is terminated by
2220 Addresses : constant In_Addr_Access_Array :=
2221 In_Addr_Access_Pointers.Value (E.H_Addr_List);
2222 -- H_Addr_List points to a list of binary addresses (in network byte
2223 -- order). The list is terminated by a NULL pointer.
2225 -- H_Length is not used because it is currently only set to 4.
2226 -- H_Addrtype is always AF_INET
2228 Result : Host_Entry_Type
2229 (Aliases_Length => Aliases'Length - 1,
2230 Addresses_Length => Addresses'Length - 1);
2231 -- The last element is a null pointer
2237 Result.Official := To_Name (Official);
2239 Source := Aliases'First;
2240 Target := Result.Aliases'First;
2241 while Target <= Result.Aliases_Length loop
2242 Result.Aliases (Target) :=
2243 To_Name (C.Strings.Value (Aliases (Source)));
2244 Source := Source + 1;
2245 Target := Target + 1;
2248 Source := Addresses'First;
2249 Target := Result.Addresses'First;
2250 while Target <= Result.Addresses_Length loop
2251 To_Inet_Addr (Addresses (Source).all, Result.Addresses (Target));
2252 Source := Source + 1;
2253 Target := Target + 1;
2263 function To_In_Addr (Addr : Inet_Addr_Type) return In_Addr is
2265 if Addr.Family = Family_Inet then
2266 return (S_B1 => C.unsigned_char (Addr.Sin_V4 (1)),
2267 S_B2 => C.unsigned_char (Addr.Sin_V4 (2)),
2268 S_B3 => C.unsigned_char (Addr.Sin_V4 (3)),
2269 S_B4 => C.unsigned_char (Addr.Sin_V4 (4)));
2272 raise Socket_Error with "IPv6 not supported";
2279 procedure To_Inet_Addr
2281 Result : out Inet_Addr_Type) is
2283 Result.Sin_V4 (1) := Inet_Addr_Comp_Type (Addr.S_B1);
2284 Result.Sin_V4 (2) := Inet_Addr_Comp_Type (Addr.S_B2);
2285 Result.Sin_V4 (3) := Inet_Addr_Comp_Type (Addr.S_B3);
2286 Result.Sin_V4 (4) := Inet_Addr_Comp_Type (Addr.S_B4);
2293 function To_Int (F : Request_Flag_Type) return C.int
2295 Current : Request_Flag_Type := F;
2296 Result : C.int := 0;
2299 for J in Flags'Range loop
2300 exit when Current = 0;
2302 if Current mod 2 /= 0 then
2303 if Flags (J) = -1 then
2304 Raise_Socket_Error (SOSC.EOPNOTSUPP);
2307 Result := Result + Flags (J);
2310 Current := Current / 2;
2320 function To_Name (N : String) return Name_Type is
2322 return Name_Type'(N'Length, N);
2325 ----------------------
2326 -- To_Service_Entry --
2327 ----------------------
2329 function To_Service_Entry (E : Servent) return Service_Entry_Type is
2332 Official : constant String := C.Strings.Value (E.S_Name);
2334 Aliases : constant Chars_Ptr_Array :=
2335 Chars_Ptr_Pointers.Value (E.S_Aliases);
2336 -- S_Aliases points to a list of name aliases. The list is
2337 -- terminated by a NULL pointer.
2339 Protocol : constant String := C.Strings.Value (E.S_Proto);
2341 Result : Service_Entry_Type (Aliases_Length => Aliases'Length - 1);
2342 -- The last element is a null pointer
2348 Result.Official := To_Name (Official);
2350 Source := Aliases'First;
2351 Target := Result.Aliases'First;
2352 while Target <= Result.Aliases_Length loop
2353 Result.Aliases (Target) :=
2354 To_Name (C.Strings.Value (Aliases (Source)));
2355 Source := Source + 1;
2356 Target := Target + 1;
2360 Port_Type (Network_To_Short (C.unsigned_short (E.S_Port)));
2362 Result.Protocol := To_Name (Protocol);
2364 end To_Service_Entry;
2370 function To_String (HN : Name_Type) return String is
2372 return HN.Name (1 .. HN.Length);
2379 function To_Timeval (Val : Timeval_Duration) return Timeval is
2384 -- If zero, set result as zero (otherwise it gets rounded down to -1)
2390 -- Normal case where we do round down
2393 S := time_t (Val - 0.5);
2394 uS := suseconds_t (1_000_000 * (Val - Selector_Duration (S)));
2405 (Stream : in out Datagram_Socket_Stream_Type;
2406 Item : Ada.Streams.Stream_Element_Array)
2409 Stream_Write (Stream.Socket, Item, To => Stream.To'Unrestricted_Access);
2417 (Stream : in out Stream_Socket_Stream_Type;
2418 Item : Ada.Streams.Stream_Element_Array)
2421 Stream_Write (Stream.Socket, Item, To => null);
2424 Sockets_Library_Controller_Object : Sockets_Library_Controller;
2425 pragma Unreferenced (Sockets_Library_Controller_Object);
2426 -- The elaboration and finalization of this object perform the required
2427 -- initialization and cleanup actions for the sockets library.