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,
1667 Msg_Iovlen => SOSC.Msg_Iovlen_T (Vector'Length),
1668 Msg_Control => System.Null_Address,
1669 Msg_Controllen => 0,
1679 if Res = ssize_t (Failure) then
1680 Raise_Socket_Error (Socket_Errno);
1683 Count := Ada.Streams.Stream_Element_Count (Res);
1690 function Resolve_Error
1691 (Error_Value : Integer;
1692 From_Errno : Boolean := True) return Error_Type
1694 use GNAT.Sockets.SOSC;
1697 if not From_Errno then
1699 when SOSC.HOST_NOT_FOUND => return Unknown_Host;
1700 when SOSC.TRY_AGAIN => return Host_Name_Lookup_Failure;
1701 when SOSC.NO_RECOVERY => return Non_Recoverable_Error;
1702 when SOSC.NO_DATA => return Unknown_Server_Error;
1703 when others => return Cannot_Resolve_Error;
1707 -- Special case: EAGAIN may be the same value as EWOULDBLOCK, so we
1708 -- can't include it in the case statement below.
1710 pragma Warnings (Off);
1711 -- Condition "EAGAIN /= EWOULDBLOCK" is known at compile time
1713 if EAGAIN /= EWOULDBLOCK and then Error_Value = EAGAIN then
1714 return Resource_Temporarily_Unavailable;
1717 pragma Warnings (On);
1720 when ENOERROR => return Success;
1721 when EACCES => return Permission_Denied;
1722 when EADDRINUSE => return Address_Already_In_Use;
1723 when EADDRNOTAVAIL => return Cannot_Assign_Requested_Address;
1724 when EAFNOSUPPORT => return
1725 Address_Family_Not_Supported_By_Protocol;
1726 when EALREADY => return Operation_Already_In_Progress;
1727 when EBADF => return Bad_File_Descriptor;
1728 when ECONNABORTED => return Software_Caused_Connection_Abort;
1729 when ECONNREFUSED => return Connection_Refused;
1730 when ECONNRESET => return Connection_Reset_By_Peer;
1731 when EDESTADDRREQ => return Destination_Address_Required;
1732 when EFAULT => return Bad_Address;
1733 when EHOSTDOWN => return Host_Is_Down;
1734 when EHOSTUNREACH => return No_Route_To_Host;
1735 when EINPROGRESS => return Operation_Now_In_Progress;
1736 when EINTR => return Interrupted_System_Call;
1737 when EINVAL => return Invalid_Argument;
1738 when EIO => return Input_Output_Error;
1739 when EISCONN => return Transport_Endpoint_Already_Connected;
1740 when ELOOP => return Too_Many_Symbolic_Links;
1741 when EMFILE => return Too_Many_Open_Files;
1742 when EMSGSIZE => return Message_Too_Long;
1743 when ENAMETOOLONG => return File_Name_Too_Long;
1744 when ENETDOWN => return Network_Is_Down;
1745 when ENETRESET => return
1746 Network_Dropped_Connection_Because_Of_Reset;
1747 when ENETUNREACH => return Network_Is_Unreachable;
1748 when ENOBUFS => return No_Buffer_Space_Available;
1749 when ENOPROTOOPT => return Protocol_Not_Available;
1750 when ENOTCONN => return Transport_Endpoint_Not_Connected;
1751 when ENOTSOCK => return Socket_Operation_On_Non_Socket;
1752 when EOPNOTSUPP => return Operation_Not_Supported;
1753 when EPFNOSUPPORT => return Protocol_Family_Not_Supported;
1754 when EPIPE => return Broken_Pipe;
1755 when EPROTONOSUPPORT => return Protocol_Not_Supported;
1756 when EPROTOTYPE => return Protocol_Wrong_Type_For_Socket;
1757 when ESHUTDOWN => return
1758 Cannot_Send_After_Transport_Endpoint_Shutdown;
1759 when ESOCKTNOSUPPORT => return Socket_Type_Not_Supported;
1760 when ETIMEDOUT => return Connection_Timed_Out;
1761 when ETOOMANYREFS => return Too_Many_References;
1762 when EWOULDBLOCK => return Resource_Temporarily_Unavailable;
1764 when others => return Cannot_Resolve_Error;
1768 -----------------------
1769 -- Resolve_Exception --
1770 -----------------------
1772 function Resolve_Exception
1773 (Occurrence : Exception_Occurrence) return Error_Type
1775 Id : constant Exception_Id := Exception_Identity (Occurrence);
1776 Msg : constant String := Exception_Message (Occurrence);
1783 while First <= Msg'Last
1784 and then Msg (First) not in '0' .. '9'
1789 if First > Msg'Last then
1790 return Cannot_Resolve_Error;
1794 while Last < Msg'Last
1795 and then Msg (Last + 1) in '0' .. '9'
1800 Val := Integer'Value (Msg (First .. Last));
1802 if Id = Socket_Error_Id then
1803 return Resolve_Error (Val);
1805 elsif Id = Host_Error_Id then
1806 return Resolve_Error (Val, False);
1809 return Cannot_Resolve_Error;
1811 end Resolve_Exception;
1817 procedure Send_Socket
1818 (Socket : Socket_Type;
1819 Item : Ada.Streams.Stream_Element_Array;
1820 Last : out Ada.Streams.Stream_Element_Offset;
1821 Flags : Request_Flag_Type := No_Request_Flag)
1824 Send_Socket (Socket, Item, Last, To => null, Flags => Flags);
1831 procedure Send_Socket
1832 (Socket : Socket_Type;
1833 Item : Ada.Streams.Stream_Element_Array;
1834 Last : out Ada.Streams.Stream_Element_Offset;
1835 To : Sock_Addr_Type;
1836 Flags : Request_Flag_Type := No_Request_Flag)
1840 (Socket, Item, Last, To => To'Unrestricted_Access, Flags => Flags);
1847 procedure Send_Socket
1848 (Socket : Socket_Type;
1849 Item : Ada.Streams.Stream_Element_Array;
1850 Last : out Ada.Streams.Stream_Element_Offset;
1851 To : access Sock_Addr_Type;
1852 Flags : Request_Flag_Type := No_Request_Flag)
1856 Sin : aliased Sockaddr_In;
1857 C_To : System.Address;
1862 Set_Family (Sin.Sin_Family, To.Family);
1863 Set_Address (Sin'Unchecked_Access, To_In_Addr (To.Addr));
1865 (Sin'Unchecked_Access,
1866 Short_To_Network (C.unsigned_short (To.Port)));
1867 C_To := Sin'Address;
1868 Len := Sin'Size / 8;
1871 C_To := System.Null_Address;
1879 Set_Forced_Flags (To_Int (Flags)),
1883 if Res = Failure then
1884 Raise_Socket_Error (Socket_Errno);
1887 Last := Last_Index (First => Item'First, Count => Res);
1894 procedure Send_Vector
1895 (Socket : Socket_Type;
1896 Vector : Vector_Type;
1897 Count : out Ada.Streams.Stream_Element_Count;
1898 Flags : Request_Flag_Type := No_Request_Flag)
1904 Iov_Count : SOSC.Msg_Iovlen_T;
1905 This_Iov_Count : SOSC.Msg_Iovlen_T;
1911 while Iov_Count < Vector'Length loop
1913 pragma Warnings (Off);
1914 -- Following test may be compile time known on some targets
1917 (if Vector'Length - Iov_Count > SOSC.IOV_MAX
1919 else Vector'Length - Iov_Count);
1921 pragma Warnings (On);
1924 (Msg_Name => System.Null_Address,
1927 (Vector'First + Integer (Iov_Count))'Address,
1928 Msg_Iovlen => This_Iov_Count,
1929 Msg_Control => System.Null_Address,
1930 Msg_Controllen => 0,
1937 Set_Forced_Flags (To_Int (Flags)));
1939 if Res = ssize_t (Failure) then
1940 Raise_Socket_Error (Socket_Errno);
1943 Count := Count + Ada.Streams.Stream_Element_Count (Res);
1944 Iov_Count := Iov_Count + This_Iov_Count;
1952 procedure Set (Item : in out Socket_Set_Type; Socket : Socket_Type) is
1954 if Item.Last = No_Socket then
1956 -- Uninitialized socket set, make sure it is properly zeroed out
1958 Reset_Socket_Set (Item.Set'Access);
1959 Item.Last := Socket;
1961 elsif Item.Last < Socket then
1962 Item.Last := Socket;
1965 Insert_Socket_In_Set (Item.Set'Access, C.int (Socket));
1968 ----------------------
1969 -- Set_Forced_Flags --
1970 ----------------------
1972 function Set_Forced_Flags (F : C.int) return C.int is
1973 use type C.unsigned;
1974 function To_unsigned is
1975 new Ada.Unchecked_Conversion (C.int, C.unsigned);
1977 new Ada.Unchecked_Conversion (C.unsigned, C.int);
1979 return To_int (To_unsigned (F) or SOSC.MSG_Forced_Flags);
1980 end Set_Forced_Flags;
1982 -----------------------
1983 -- Set_Socket_Option --
1984 -----------------------
1986 procedure Set_Socket_Option
1987 (Socket : Socket_Type;
1988 Level : Level_Type := Socket_Level;
1989 Option : Option_Type)
1991 V8 : aliased Two_Ints;
1993 V1 : aliased C.unsigned_char;
1994 VT : aliased Timeval;
1996 Add : System.Address := Null_Address;
2005 V4 := C.int (Boolean'Pos (Option.Enabled));
2010 V8 (V8'First) := C.int (Boolean'Pos (Option.Enabled));
2011 V8 (V8'Last) := C.int (Option.Seconds);
2017 V4 := C.int (Option.Size);
2022 V4 := C.int (Boolean'Pos (True));
2026 when Add_Membership |
2028 V8 (V8'First) := To_Int (To_In_Addr (Option.Multicast_Address));
2029 V8 (V8'Last) := To_Int (To_In_Addr (Option.Local_Interface));
2033 when Multicast_If =>
2034 V4 := To_Int (To_In_Addr (Option.Outgoing_If));
2038 when Multicast_TTL =>
2039 V1 := C.unsigned_char (Option.Time_To_Live);
2043 when Multicast_Loop |
2044 Receive_Packet_Info =>
2045 V1 := C.unsigned_char (Boolean'Pos (Option.Enabled));
2051 VT := To_Timeval (Option.Timeout);
2060 Options (Option.Name),
2063 if Res = Failure then
2064 Raise_Socket_Error (Socket_Errno);
2066 end Set_Socket_Option;
2068 ----------------------
2069 -- Short_To_Network --
2070 ----------------------
2072 function Short_To_Network (S : C.unsigned_short) return C.unsigned_short is
2073 use type C.unsigned_short;
2076 -- Big-endian case. No conversion needed. On these platforms,
2077 -- htons() defaults to a null procedure.
2079 pragma Warnings (Off);
2080 -- Since the test can generate "always True/False" warning
2082 if Default_Bit_Order = High_Order_First then
2085 pragma Warnings (On);
2087 -- Little-endian case. We must swap the high and low bytes of this
2088 -- short to make the port number network compliant.
2091 return (S / 256) + (S mod 256) * 256;
2093 end Short_To_Network;
2095 ---------------------
2096 -- Shutdown_Socket --
2097 ---------------------
2099 procedure Shutdown_Socket
2100 (Socket : Socket_Type;
2101 How : Shutmode_Type := Shut_Read_Write)
2106 Res := C_Shutdown (C.int (Socket), Shutmodes (How));
2108 if Res = Failure then
2109 Raise_Socket_Error (Socket_Errno);
2111 end Shutdown_Socket;
2118 (Socket : Socket_Type;
2119 Send_To : Sock_Addr_Type) return Stream_Access
2121 S : Datagram_Socket_Stream_Access;
2124 S := new Datagram_Socket_Stream_Type;
2127 S.From := Get_Socket_Name (Socket);
2128 return Stream_Access (S);
2135 function Stream (Socket : Socket_Type) return Stream_Access is
2136 S : Stream_Socket_Stream_Access;
2138 S := new Stream_Socket_Stream_Type;
2140 return Stream_Access (S);
2147 procedure Stream_Write
2148 (Socket : Socket_Type;
2149 Item : Ada.Streams.Stream_Element_Array;
2150 To : access Sock_Addr_Type)
2152 First : Ada.Streams.Stream_Element_Offset;
2153 Index : Ada.Streams.Stream_Element_Offset;
2154 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
2157 First := Item'First;
2159 while First <= Max loop
2160 Send_Socket (Socket, Item (First .. Max), Index, To);
2162 -- Exit when all or zero data sent. Zero means that the socket has
2163 -- been closed by peer.
2165 exit when Index < First or else Index = Max;
2170 -- For an empty array, we have First > Max, and hence Index >= Max (no
2171 -- error, the loop above is never executed). After a succesful send,
2172 -- Index = Max. The only remaining case, Index < Max, is therefore
2173 -- always an actual send failure.
2176 Raise_Socket_Error (Socket_Errno);
2184 function To_C (Socket : Socket_Type) return Integer is
2186 return Integer (Socket);
2193 function To_Duration (Val : Timeval) return Timeval_Duration is
2195 return Natural (Val.Tv_Sec) * 1.0 + Natural (Val.Tv_Usec) * 1.0E-6;
2202 function To_Host_Entry (E : Hostent) return Host_Entry_Type is
2205 Official : constant String :=
2206 C.Strings.Value (E.H_Name);
2208 Aliases : constant Chars_Ptr_Array :=
2209 Chars_Ptr_Pointers.Value (E.H_Aliases);
2210 -- H_Aliases points to a list of name aliases. The list is terminated by
2213 Addresses : constant In_Addr_Access_Array :=
2214 In_Addr_Access_Pointers.Value (E.H_Addr_List);
2215 -- H_Addr_List points to a list of binary addresses (in network byte
2216 -- order). The list is terminated by a NULL pointer.
2218 -- H_Length is not used because it is currently only set to 4.
2219 -- H_Addrtype is always AF_INET
2221 Result : Host_Entry_Type
2222 (Aliases_Length => Aliases'Length - 1,
2223 Addresses_Length => Addresses'Length - 1);
2224 -- The last element is a null pointer
2230 Result.Official := To_Name (Official);
2232 Source := Aliases'First;
2233 Target := Result.Aliases'First;
2234 while Target <= Result.Aliases_Length loop
2235 Result.Aliases (Target) :=
2236 To_Name (C.Strings.Value (Aliases (Source)));
2237 Source := Source + 1;
2238 Target := Target + 1;
2241 Source := Addresses'First;
2242 Target := Result.Addresses'First;
2243 while Target <= Result.Addresses_Length loop
2244 To_Inet_Addr (Addresses (Source).all, Result.Addresses (Target));
2245 Source := Source + 1;
2246 Target := Target + 1;
2256 function To_In_Addr (Addr : Inet_Addr_Type) return In_Addr is
2258 if Addr.Family = Family_Inet then
2259 return (S_B1 => C.unsigned_char (Addr.Sin_V4 (1)),
2260 S_B2 => C.unsigned_char (Addr.Sin_V4 (2)),
2261 S_B3 => C.unsigned_char (Addr.Sin_V4 (3)),
2262 S_B4 => C.unsigned_char (Addr.Sin_V4 (4)));
2265 raise Socket_Error with "IPv6 not supported";
2272 procedure To_Inet_Addr
2274 Result : out Inet_Addr_Type) is
2276 Result.Sin_V4 (1) := Inet_Addr_Comp_Type (Addr.S_B1);
2277 Result.Sin_V4 (2) := Inet_Addr_Comp_Type (Addr.S_B2);
2278 Result.Sin_V4 (3) := Inet_Addr_Comp_Type (Addr.S_B3);
2279 Result.Sin_V4 (4) := Inet_Addr_Comp_Type (Addr.S_B4);
2286 function To_Int (F : Request_Flag_Type) return C.int
2288 Current : Request_Flag_Type := F;
2289 Result : C.int := 0;
2292 for J in Flags'Range loop
2293 exit when Current = 0;
2295 if Current mod 2 /= 0 then
2296 if Flags (J) = -1 then
2297 Raise_Socket_Error (SOSC.EOPNOTSUPP);
2300 Result := Result + Flags (J);
2303 Current := Current / 2;
2313 function To_Name (N : String) return Name_Type is
2315 return Name_Type'(N'Length, N);
2318 ----------------------
2319 -- To_Service_Entry --
2320 ----------------------
2322 function To_Service_Entry (E : Servent) return Service_Entry_Type is
2325 Official : constant String := C.Strings.Value (E.S_Name);
2327 Aliases : constant Chars_Ptr_Array :=
2328 Chars_Ptr_Pointers.Value (E.S_Aliases);
2329 -- S_Aliases points to a list of name aliases. The list is
2330 -- terminated by a NULL pointer.
2332 Protocol : constant String := C.Strings.Value (E.S_Proto);
2334 Result : Service_Entry_Type (Aliases_Length => Aliases'Length - 1);
2335 -- The last element is a null pointer
2341 Result.Official := To_Name (Official);
2343 Source := Aliases'First;
2344 Target := Result.Aliases'First;
2345 while Target <= Result.Aliases_Length loop
2346 Result.Aliases (Target) :=
2347 To_Name (C.Strings.Value (Aliases (Source)));
2348 Source := Source + 1;
2349 Target := Target + 1;
2353 Port_Type (Network_To_Short (C.unsigned_short (E.S_Port)));
2355 Result.Protocol := To_Name (Protocol);
2357 end To_Service_Entry;
2363 function To_String (HN : Name_Type) return String is
2365 return HN.Name (1 .. HN.Length);
2372 function To_Timeval (Val : Timeval_Duration) return Timeval is
2377 -- If zero, set result as zero (otherwise it gets rounded down to -1)
2383 -- Normal case where we do round down
2386 S := time_t (Val - 0.5);
2387 uS := suseconds_t (1_000_000 * (Val - Selector_Duration (S)));
2398 (Stream : in out Datagram_Socket_Stream_Type;
2399 Item : Ada.Streams.Stream_Element_Array)
2402 Stream_Write (Stream.Socket, Item, To => Stream.To'Unrestricted_Access);
2410 (Stream : in out Stream_Socket_Stream_Type;
2411 Item : Ada.Streams.Stream_Element_Array)
2414 Stream_Write (Stream.Socket, Item, To => null);
2417 Sockets_Library_Controller_Object : Sockets_Library_Controller;
2418 pragma Unreferenced (Sockets_Library_Controller_Object);
2419 -- The elaboration and finalization of this object perform the required
2420 -- initialization and cleanup actions for the sockets library.