1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S --
9 -- Copyright (C) 2001-2008, 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 Empty_Socket_Set : Socket_Set_Type;
60 -- Variable set in Initialize, and then used internally to provide an
61 -- initial value for Socket_Set_Type objects.
63 Netdb_Buffer_Size : constant := SOSC.Need_Netdb_Buffer * 1024;
64 -- The network database functions gethostbyname, gethostbyaddr,
65 -- getservbyname and getservbyport can either be guaranteed task safe by
66 -- the operating system, or else return data through a user-provided buffer
67 -- to ensure concurrent uses do not interfere.
69 -- Correspondence tables
71 Levels : constant array (Level_Type) of C.int :=
72 (Socket_Level => SOSC.SOL_SOCKET,
73 IP_Protocol_For_IP_Level => SOSC.IPPROTO_IP,
74 IP_Protocol_For_UDP_Level => SOSC.IPPROTO_UDP,
75 IP_Protocol_For_TCP_Level => SOSC.IPPROTO_TCP);
77 Modes : constant array (Mode_Type) of C.int :=
78 (Socket_Stream => SOSC.SOCK_STREAM,
79 Socket_Datagram => SOSC.SOCK_DGRAM);
81 Shutmodes : constant array (Shutmode_Type) of C.int :=
82 (Shut_Read => SOSC.SHUT_RD,
83 Shut_Write => SOSC.SHUT_WR,
84 Shut_Read_Write => SOSC.SHUT_RDWR);
86 Requests : constant array (Request_Name) of C.int :=
87 (Non_Blocking_IO => SOSC.FIONBIO,
88 N_Bytes_To_Read => SOSC.FIONREAD);
90 Options : constant array (Option_Name) of C.int :=
91 (Keep_Alive => SOSC.SO_KEEPALIVE,
92 Reuse_Address => SOSC.SO_REUSEADDR,
93 Broadcast => SOSC.SO_BROADCAST,
94 Send_Buffer => SOSC.SO_SNDBUF,
95 Receive_Buffer => SOSC.SO_RCVBUF,
96 Linger => SOSC.SO_LINGER,
97 Error => SOSC.SO_ERROR,
98 No_Delay => SOSC.TCP_NODELAY,
99 Add_Membership => SOSC.IP_ADD_MEMBERSHIP,
100 Drop_Membership => SOSC.IP_DROP_MEMBERSHIP,
101 Multicast_If => SOSC.IP_MULTICAST_IF,
102 Multicast_TTL => SOSC.IP_MULTICAST_TTL,
103 Multicast_Loop => SOSC.IP_MULTICAST_LOOP,
104 Receive_Packet_Info => SOSC.IP_PKTINFO,
105 Send_Timeout => SOSC.SO_SNDTIMEO,
106 Receive_Timeout => SOSC.SO_RCVTIMEO);
107 -- ??? Note: for OpenSolaris, Receive_Packet_Info should be IP_RECVPKTINFO,
108 -- but for Linux compatibility this constant is the same as IP_PKTINFO.
110 Flags : constant array (0 .. 3) of C.int :=
111 (0 => SOSC.MSG_OOB, -- Process_Out_Of_Band_Data
112 1 => SOSC.MSG_PEEK, -- Peek_At_Incoming_Data
113 2 => SOSC.MSG_WAITALL, -- Wait_For_A_Full_Reception
114 3 => SOSC.MSG_EOR); -- Send_End_Of_Record
116 Socket_Error_Id : constant Exception_Id := Socket_Error'Identity;
117 Host_Error_Id : constant Exception_Id := Host_Error'Identity;
119 Hex_To_Char : constant String (1 .. 16) := "0123456789ABCDEF";
120 -- Use to print in hexadecimal format
122 function Err_Code_Image (E : Integer) return String;
123 -- Return the value of E surrounded with brackets
125 -----------------------
126 -- Local subprograms --
127 -----------------------
129 function Resolve_Error
130 (Error_Value : Integer;
131 From_Errno : Boolean := True) return Error_Type;
132 -- Associate an enumeration value (error_type) to en error value (errno).
133 -- From_Errno prevents from mixing h_errno with errno.
135 function To_Name (N : String) return Name_Type;
136 function To_String (HN : Name_Type) return String;
137 -- Conversion functions
139 function To_Int (F : Request_Flag_Type) return C.int;
140 -- Return the int value corresponding to the specified flags combination
142 function Set_Forced_Flags (F : C.int) return C.int;
143 -- Return F with the bits from SOSC.MSG_Forced_Flags forced set
145 function Short_To_Network
146 (S : C.unsigned_short) return C.unsigned_short;
147 pragma Inline (Short_To_Network);
148 -- Convert a port number into a network port number
150 function Network_To_Short
151 (S : C.unsigned_short) return C.unsigned_short
152 renames Short_To_Network;
153 -- Symmetric operation
156 (Val : Inet_Addr_VN_Type;
157 Hex : Boolean := False) return String;
158 -- Output an array of inet address components in hex or decimal mode
160 function Is_IP_Address (Name : String) return Boolean;
161 -- Return true when Name is an IP address in standard dot notation
163 function To_In_Addr (Addr : Inet_Addr_Type) return In_Addr;
164 procedure To_Inet_Addr
166 Result : out Inet_Addr_Type);
167 -- Conversion functions
169 function To_Host_Entry (E : Hostent) return Host_Entry_Type;
170 -- Conversion function
172 function To_Service_Entry (E : Servent) return Service_Entry_Type;
173 -- Conversion function
175 function To_Timeval (Val : Timeval_Duration) return Timeval;
176 -- Separate Val in seconds and microseconds
178 function To_Duration (Val : Timeval) return Timeval_Duration;
179 -- Reconstruct a Duration value from a Timeval record (seconds and
182 procedure Raise_Socket_Error (Error : Integer);
183 -- Raise Socket_Error with an exception message describing the error code
186 procedure Raise_Host_Error (H_Error : Integer);
187 -- Raise Host_Error exception with message describing error code (note
188 -- hstrerror seems to be obsolete) from h_errno.
190 procedure Narrow (Item : in out Socket_Set_Type);
191 -- Update Last as it may be greater than the real last socket
193 -- Types needed for Datagram_Socket_Stream_Type
195 type Datagram_Socket_Stream_Type is new Root_Stream_Type with record
196 Socket : Socket_Type;
198 From : Sock_Addr_Type;
201 type Datagram_Socket_Stream_Access is
202 access all Datagram_Socket_Stream_Type;
205 (Stream : in out Datagram_Socket_Stream_Type;
206 Item : out Ada.Streams.Stream_Element_Array;
207 Last : out Ada.Streams.Stream_Element_Offset);
210 (Stream : in out Datagram_Socket_Stream_Type;
211 Item : Ada.Streams.Stream_Element_Array);
213 -- Types needed for Stream_Socket_Stream_Type
215 type Stream_Socket_Stream_Type is new Root_Stream_Type with record
216 Socket : Socket_Type;
219 type Stream_Socket_Stream_Access is
220 access all Stream_Socket_Stream_Type;
223 (Stream : in out Stream_Socket_Stream_Type;
224 Item : out Ada.Streams.Stream_Element_Array;
225 Last : out Ada.Streams.Stream_Element_Offset);
228 (Stream : in out Stream_Socket_Stream_Type;
229 Item : Ada.Streams.Stream_Element_Array);
231 procedure Stream_Write
232 (Socket : Socket_Type;
233 Item : Ada.Streams.Stream_Element_Array;
234 To : access Sock_Addr_Type);
235 -- Common implementation for the Write operation of Datagram_Socket_Stream_
236 -- Type and Stream_Socket_Stream_Type.
238 procedure Wait_On_Socket
239 (Socket : Socket_Type;
241 Timeout : Selector_Duration;
242 Selector : access Selector_Type := null;
243 Status : out Selector_Status);
244 -- Common code for variants of socket operations supporting a timeout:
245 -- block in Check_Selector on Socket for at most the indicated timeout.
246 -- If For_Read is True, Socket is added to the read set for this call, else
247 -- it is added to the write set. If no selector is provided, a local one is
248 -- created for this call and destroyed prior to returning.
250 type Sockets_Library_Controller is new Ada.Finalization.Limited_Controlled
252 -- This type is used to generate automatic calls to Initialize and Finalize
253 -- during the elaboration and finalization of this package. A single object
254 -- of this type must exist at library level.
256 procedure Initialize (X : in out Sockets_Library_Controller);
257 procedure Finalize (X : in out Sockets_Library_Controller);
263 function "+" (L, R : Request_Flag_Type) return Request_Flag_Type is
272 procedure Abort_Selector (Selector : Selector_Type) is
276 -- Send one byte to unblock select system call
278 Res := Signalling_Fds.Write (C.int (Selector.W_Sig_Socket));
280 if Res = Failure then
281 Raise_Socket_Error (Socket_Errno);
289 procedure Accept_Socket
290 (Server : Socket_Type;
291 Socket : out Socket_Type;
292 Address : out Sock_Addr_Type)
295 Sin : aliased Sockaddr_In;
296 Len : aliased C.int := Sin'Size / 8;
299 Res := C_Accept (C.int (Server), Sin'Address, Len'Access);
301 if Res = Failure then
302 Raise_Socket_Error (Socket_Errno);
305 Socket := Socket_Type (Res);
307 To_Inet_Addr (Sin.Sin_Addr, Address.Addr);
308 Address.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
315 procedure Accept_Socket
316 (Server : Socket_Type;
317 Socket : out Socket_Type;
318 Address : out Sock_Addr_Type;
319 Timeout : Selector_Duration;
320 Selector : access Selector_Type := null;
321 Status : out Selector_Status)
324 -- Wait for socket to become available for reading
330 Selector => Selector,
333 -- Accept connection if available
335 if Status = Completed then
336 Accept_Socket (Server, Socket, Address);
347 (E : Host_Entry_Type;
348 N : Positive := 1) return Inet_Addr_Type
351 return E.Addresses (N);
354 ----------------------
355 -- Addresses_Length --
356 ----------------------
358 function Addresses_Length (E : Host_Entry_Type) return Natural is
360 return E.Addresses_Length;
361 end Addresses_Length;
368 (E : Host_Entry_Type;
369 N : Positive := 1) return String
372 return To_String (E.Aliases (N));
380 (S : Service_Entry_Type;
381 N : Positive := 1) return String
384 return To_String (S.Aliases (N));
391 function Aliases_Length (E : Host_Entry_Type) return Natural is
393 return E.Aliases_Length;
400 function Aliases_Length (S : Service_Entry_Type) return Natural is
402 return S.Aliases_Length;
409 procedure Bind_Socket
410 (Socket : Socket_Type;
411 Address : Sock_Addr_Type)
414 Sin : aliased Sockaddr_In;
415 Len : constant C.int := Sin'Size / 8;
416 -- This assumes that Address.Family = Family_Inet???
419 if Address.Family = Family_Inet6 then
420 raise Socket_Error with "IPv6 not supported";
423 Set_Family (Sin.Sin_Family, Address.Family);
424 Set_Address (Sin'Unchecked_Access, To_In_Addr (Address.Addr));
426 (Sin'Unchecked_Access,
427 Short_To_Network (C.unsigned_short (Address.Port)));
429 Res := C_Bind (C.int (Socket), Sin'Address, Len);
431 if Res = Failure then
432 Raise_Socket_Error (Socket_Errno);
440 procedure Check_Selector
441 (Selector : in out Selector_Type;
442 R_Socket_Set : in out Socket_Set_Type;
443 W_Socket_Set : in out Socket_Set_Type;
444 Status : out Selector_Status;
445 Timeout : Selector_Duration := Forever)
447 E_Socket_Set : Socket_Set_Type := Empty_Socket_Set;
450 (Selector, R_Socket_Set, W_Socket_Set, E_Socket_Set, Status, Timeout);
457 procedure Check_Selector
458 (Selector : in out Selector_Type;
459 R_Socket_Set : in out Socket_Set_Type;
460 W_Socket_Set : in out Socket_Set_Type;
461 E_Socket_Set : in out Socket_Set_Type;
462 Status : out Selector_Status;
463 Timeout : Selector_Duration := Forever)
467 RSig : Socket_Type renames Selector.R_Sig_Socket;
468 RSet : Socket_Set_Type;
469 WSet : Socket_Set_Type;
470 ESet : Socket_Set_Type;
471 TVal : aliased Timeval;
472 TPtr : Timeval_Access;
477 -- No timeout or Forever is indicated by a null timeval pointer
479 if Timeout = Forever then
482 TVal := To_Timeval (Timeout);
483 TPtr := TVal'Unchecked_Access;
486 -- Copy R_Socket_Set in RSet and add read signalling socket
488 RSet := R_Socket_Set;
491 -- Copy W_Socket_Set in WSet
493 WSet := W_Socket_Set;
495 -- Copy E_Socket_Set in ESet
497 ESet := E_Socket_Set;
499 Last := C.int'Max (C.int'Max (C.int (RSet.Last),
511 if Res = Failure then
512 Raise_Socket_Error (Socket_Errno);
515 -- If Select was resumed because of read signalling socket, read this
516 -- data and remove socket from set.
518 if Is_Set (RSet, RSig) then
521 Res := Signalling_Fds.Read (C.int (RSig));
523 if Res = Failure then
524 Raise_Socket_Error (Socket_Errno);
533 -- Update RSet, WSet and ESet in regard to their new socket sets
539 -- Reset RSet as it should be if R_Sig_Socket was not added
541 if Is_Empty (RSet) then
545 if Is_Empty (WSet) then
549 if Is_Empty (ESet) then
553 -- Deliver RSet, WSet and ESet
555 R_Socket_Set := RSet;
556 W_Socket_Set := WSet;
557 E_Socket_Set := ESet;
565 (Item : in out Socket_Set_Type;
566 Socket : Socket_Type)
568 Last : aliased C.int := C.int (Item.Last);
570 if Item.Last /= No_Socket then
571 Remove_Socket_From_Set (Item.Set'Access, C.int (Socket));
572 Last_Socket_In_Set (Item.Set'Access, Last'Unchecked_Access);
573 Item.Last := Socket_Type (Last);
581 procedure Close_Selector (Selector : in out Selector_Type) is
583 -- Close the signalling file descriptors used internally for the
584 -- implementation of Abort_Selector.
586 Signalling_Fds.Close (C.int (Selector.R_Sig_Socket));
587 Signalling_Fds.Close (C.int (Selector.W_Sig_Socket));
589 -- Reset R_Sig_Socket and W_Sig_Socket to No_Socket to ensure that any
590 -- (erroneous) subsequent attempt to use this selector properly fails.
592 Selector.R_Sig_Socket := No_Socket;
593 Selector.W_Sig_Socket := No_Socket;
600 procedure Close_Socket (Socket : Socket_Type) is
604 Res := C_Close (C.int (Socket));
606 if Res = Failure then
607 Raise_Socket_Error (Socket_Errno);
615 procedure Connect_Socket
616 (Socket : Socket_Type;
617 Server : Sock_Addr_Type)
620 Sin : aliased Sockaddr_In;
621 Len : constant C.int := Sin'Size / 8;
624 if Server.Family = Family_Inet6 then
625 raise Socket_Error with "IPv6 not supported";
628 Set_Family (Sin.Sin_Family, Server.Family);
629 Set_Address (Sin'Unchecked_Access, To_In_Addr (Server.Addr));
631 (Sin'Unchecked_Access,
632 Short_To_Network (C.unsigned_short (Server.Port)));
634 Res := C_Connect (C.int (Socket), Sin'Address, Len);
636 if Res = Failure then
637 Raise_Socket_Error (Socket_Errno);
645 procedure Connect_Socket
646 (Socket : Socket_Type;
647 Server : Sock_Addr_Type;
648 Timeout : Selector_Duration;
649 Selector : access Selector_Type := null;
650 Status : out Selector_Status)
653 -- Used to set Socket to non-blocking I/O
656 -- Set the socket to non-blocking I/O
658 Req := (Name => Non_Blocking_IO, Enabled => True);
659 Control_Socket (Socket, Request => Req);
661 -- Start operation (non-blocking), will raise Socket_Error with
665 Connect_Socket (Socket, Server);
667 when E : Socket_Error =>
668 if Resolve_Exception (E) = Operation_Now_In_Progress then
675 -- Wait for socket to become available for writing
681 Selector => Selector,
684 -- Reset the socket to blocking I/O
686 Req := (Name => Non_Blocking_IO, Enabled => False);
687 Control_Socket (Socket, Request => Req);
694 procedure Control_Socket
695 (Socket : Socket_Type;
696 Request : in out Request_Type)
703 when Non_Blocking_IO =>
704 Arg := C.int (Boolean'Pos (Request.Enabled));
706 when N_Bytes_To_Read =>
712 Requests (Request.Name),
713 Arg'Unchecked_Access);
715 if Res = Failure then
716 Raise_Socket_Error (Socket_Errno);
720 when Non_Blocking_IO =>
723 when N_Bytes_To_Read =>
724 Request.Size := Natural (Arg);
733 (Source : Socket_Set_Type;
734 Target : in out Socket_Set_Type)
740 ---------------------
741 -- Create_Selector --
742 ---------------------
744 procedure Create_Selector (Selector : out Selector_Type) is
745 Two_Fds : aliased Fd_Pair;
749 -- We open two signalling file descriptors. One of them is used to send
750 -- data to the other, which is included in a C_Select socket set. The
751 -- communication is used to force a call to C_Select to complete, and
752 -- the waiting task to resume its execution.
754 Res := Signalling_Fds.Create (Two_Fds'Access);
756 if Res = Failure then
757 Raise_Socket_Error (Socket_Errno);
760 Selector.R_Sig_Socket := Socket_Type (Two_Fds (Read_End));
761 Selector.W_Sig_Socket := Socket_Type (Two_Fds (Write_End));
768 procedure Create_Socket
769 (Socket : out Socket_Type;
770 Family : Family_Type := Family_Inet;
771 Mode : Mode_Type := Socket_Stream)
776 Res := C_Socket (Families (Family), Modes (Mode), 0);
778 if Res = Failure then
779 Raise_Socket_Error (Socket_Errno);
782 Socket := Socket_Type (Res);
789 procedure Empty (Item : in out Socket_Set_Type) is
791 Reset_Socket_Set (Item.Set'Access);
792 Item.Last := No_Socket;
799 function Err_Code_Image (E : Integer) return String is
800 Msg : String := E'Img & "] ";
802 Msg (Msg'First) := '[';
810 procedure Finalize (X : in out Sockets_Library_Controller) is
811 pragma Unreferenced (X);
814 -- Finalization operation for the GNAT.Sockets package
823 procedure Finalize is
825 -- This is a dummy placeholder for an obsolete API.
826 -- The real finalization actions are in Initialize primitive operation
827 -- of Sockets_Library_Controller.
837 (Item : in out Socket_Set_Type;
838 Socket : out Socket_Type)
841 L : aliased C.int := C.int (Item.Last);
844 if Item.Last /= No_Socket then
846 (Item.Set'Access, Last => L'Access, Socket => S'Access);
847 Item.Last := Socket_Type (L);
848 Socket := Socket_Type (S);
859 (Stream : not null Stream_Access) return Sock_Addr_Type
862 if Stream.all in Datagram_Socket_Stream_Type then
863 return Datagram_Socket_Stream_Type (Stream.all).From;
865 return Get_Peer_Name (Stream_Socket_Stream_Type (Stream.all).Socket);
869 -------------------------
870 -- Get_Host_By_Address --
871 -------------------------
873 function Get_Host_By_Address
874 (Address : Inet_Addr_Type;
875 Family : Family_Type := Family_Inet) return Host_Entry_Type
877 pragma Unreferenced (Family);
879 HA : aliased In_Addr := To_In_Addr (Address);
880 Buflen : constant C.int := Netdb_Buffer_Size;
881 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
882 Res : aliased Hostent;
886 if Safe_Gethostbyaddr (HA'Address, HA'Size / 8, SOSC.AF_INET,
887 Res'Access, Buf'Address, Buflen, Err'Access) /= 0
889 Raise_Host_Error (Integer (Err));
892 return To_Host_Entry (Res);
893 end Get_Host_By_Address;
895 ----------------------
896 -- Get_Host_By_Name --
897 ----------------------
899 function Get_Host_By_Name (Name : String) return Host_Entry_Type is
901 -- Detect IP address name and redirect to Inet_Addr
903 if Is_IP_Address (Name) then
904 return Get_Host_By_Address (Inet_Addr (Name));
908 HN : constant C.char_array := C.To_C (Name);
909 Buflen : constant C.int := Netdb_Buffer_Size;
910 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
911 Res : aliased Hostent;
915 if Safe_Gethostbyname
916 (HN, Res'Access, Buf'Address, Buflen, Err'Access) /= 0
918 Raise_Host_Error (Integer (Err));
921 return To_Host_Entry (Res);
923 end Get_Host_By_Name;
929 function Get_Peer_Name (Socket : Socket_Type) return Sock_Addr_Type is
930 Sin : aliased Sockaddr_In;
931 Len : aliased C.int := Sin'Size / 8;
932 Res : Sock_Addr_Type (Family_Inet);
935 if C_Getpeername (C.int (Socket), Sin'Address, Len'Access) = Failure then
936 Raise_Socket_Error (Socket_Errno);
939 To_Inet_Addr (Sin.Sin_Addr, Res.Addr);
940 Res.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
945 -------------------------
946 -- Get_Service_By_Name --
947 -------------------------
949 function Get_Service_By_Name
951 Protocol : String) return Service_Entry_Type
953 SN : constant C.char_array := C.To_C (Name);
954 SP : constant C.char_array := C.To_C (Protocol);
955 Buflen : constant C.int := Netdb_Buffer_Size;
956 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
957 Res : aliased Servent;
960 if Safe_Getservbyname (SN, SP, Res'Access, Buf'Address, Buflen) /= 0 then
961 raise Service_Error with "Service not found";
964 -- Translate from the C format to the API format
966 return To_Service_Entry (Res);
967 end Get_Service_By_Name;
969 -------------------------
970 -- Get_Service_By_Port --
971 -------------------------
973 function Get_Service_By_Port
975 Protocol : String) return Service_Entry_Type
977 SP : constant C.char_array := C.To_C (Protocol);
978 Buflen : constant C.int := Netdb_Buffer_Size;
979 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
980 Res : aliased Servent;
983 if Safe_Getservbyport
984 (C.int (Short_To_Network (C.unsigned_short (Port))), SP,
985 Res'Access, Buf'Address, Buflen) /= 0
987 raise Service_Error with "Service not found";
990 -- Translate from the C format to the API format
992 return To_Service_Entry (Res);
993 end Get_Service_By_Port;
995 ---------------------
996 -- Get_Socket_Name --
997 ---------------------
999 function Get_Socket_Name
1000 (Socket : Socket_Type) return Sock_Addr_Type
1002 Sin : aliased Sockaddr_In;
1003 Len : aliased C.int := Sin'Size / 8;
1005 Addr : Sock_Addr_Type := No_Sock_Addr;
1008 Res := C_Getsockname (C.int (Socket), Sin'Address, Len'Access);
1010 if Res /= Failure then
1011 To_Inet_Addr (Sin.Sin_Addr, Addr.Addr);
1012 Addr.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
1016 end Get_Socket_Name;
1018 -----------------------
1019 -- Get_Socket_Option --
1020 -----------------------
1022 function Get_Socket_Option
1023 (Socket : Socket_Type;
1024 Level : Level_Type := Socket_Level;
1025 Name : Option_Name) return Option_Type
1027 use type C.unsigned_char;
1029 V8 : aliased Two_Ints;
1031 V1 : aliased C.unsigned_char;
1032 VT : aliased Timeval;
1033 Len : aliased C.int;
1034 Add : System.Address;
1036 Opt : Option_Type (Name);
1040 when Multicast_Loop |
1042 Receive_Packet_Info =>
1077 if Res = Failure then
1078 Raise_Socket_Error (Socket_Errno);
1086 Opt.Enabled := (V4 /= 0);
1089 Opt.Enabled := (V8 (V8'First) /= 0);
1090 Opt.Seconds := Natural (V8 (V8'Last));
1094 Opt.Size := Natural (V4);
1097 Opt.Error := Resolve_Error (Integer (V4));
1099 when Add_Membership |
1101 To_Inet_Addr (To_In_Addr (V8 (V8'First)), Opt.Multicast_Address);
1102 To_Inet_Addr (To_In_Addr (V8 (V8'Last)), Opt.Local_Interface);
1104 when Multicast_If =>
1105 To_Inet_Addr (To_In_Addr (V4), Opt.Outgoing_If);
1107 when Multicast_TTL =>
1108 Opt.Time_To_Live := Integer (V1);
1110 when Multicast_Loop |
1111 Receive_Packet_Info =>
1112 Opt.Enabled := (V1 /= 0);
1116 Opt.Timeout := To_Duration (VT);
1120 end Get_Socket_Option;
1126 function Host_Name return String is
1127 Name : aliased C.char_array (1 .. 64);
1131 Res := C_Gethostname (Name'Address, Name'Length);
1133 if Res = Failure then
1134 Raise_Socket_Error (Socket_Errno);
1137 return C.To_Ada (Name);
1145 (Val : Inet_Addr_VN_Type;
1146 Hex : Boolean := False) return String
1148 -- The largest Inet_Addr_Comp_Type image occurs with IPv4. It
1149 -- has at most a length of 3 plus one '.' character.
1151 Buffer : String (1 .. 4 * Val'Length);
1152 Length : Natural := 1;
1153 Separator : Character;
1155 procedure Img10 (V : Inet_Addr_Comp_Type);
1156 -- Append to Buffer image of V in decimal format
1158 procedure Img16 (V : Inet_Addr_Comp_Type);
1159 -- Append to Buffer image of V in hexadecimal format
1165 procedure Img10 (V : Inet_Addr_Comp_Type) is
1166 Img : constant String := V'Img;
1167 Len : constant Natural := Img'Length - 1;
1169 Buffer (Length .. Length + Len - 1) := Img (2 .. Img'Last);
1170 Length := Length + Len;
1177 procedure Img16 (V : Inet_Addr_Comp_Type) is
1179 Buffer (Length) := Hex_To_Char (Natural (V / 16) + 1);
1180 Buffer (Length + 1) := Hex_To_Char (Natural (V mod 16) + 1);
1181 Length := Length + 2;
1184 -- Start of processing for Image
1193 for J in Val'Range loop
1200 if J /= Val'Last then
1201 Buffer (Length) := Separator;
1202 Length := Length + 1;
1206 return Buffer (1 .. Length - 1);
1213 function Image (Value : Inet_Addr_Type) return String is
1215 if Value.Family = Family_Inet then
1216 return Image (Inet_Addr_VN_Type (Value.Sin_V4), Hex => False);
1218 return Image (Inet_Addr_VN_Type (Value.Sin_V6), Hex => True);
1226 function Image (Value : Sock_Addr_Type) return String is
1227 Port : constant String := Value.Port'Img;
1229 return Image (Value.Addr) & ':' & Port (2 .. Port'Last);
1236 function Image (Socket : Socket_Type) return String is
1245 function Image (Item : Socket_Set_Type) return String is
1246 Socket_Set : Socket_Set_Type := Item;
1250 Last_Img : constant String := Socket_Set.Last'Img;
1252 (1 .. (Integer (Socket_Set.Last) + 1) * Last_Img'Length);
1253 Index : Positive := 1;
1254 Socket : Socket_Type;
1257 while not Is_Empty (Socket_Set) loop
1258 Get (Socket_Set, Socket);
1261 Socket_Img : constant String := Socket'Img;
1263 Buffer (Index .. Index + Socket_Img'Length - 1) := Socket_Img;
1264 Index := Index + Socket_Img'Length;
1268 return "[" & Last_Img & "]" & Buffer (1 .. Index - 1);
1276 function Inet_Addr (Image : String) return Inet_Addr_Type is
1277 use Interfaces.C.Strings;
1281 Result : Inet_Addr_Type;
1284 -- Special case for the all-ones broadcast address: this address has the
1285 -- same in_addr_t value as Failure, and thus cannot be properly returned
1288 if Image = "255.255.255.255" then
1289 return Broadcast_Inet_Addr;
1291 -- Special case for an empty Image as on some platforms (e.g. Windows)
1292 -- calling Inet_Addr("") will not return an error.
1294 elsif Image = "" then
1295 Raise_Socket_Error (SOSC.EINVAL);
1298 Img := New_String (Image);
1299 Res := C_Inet_Addr (Img);
1302 if Res = Failure then
1303 Raise_Socket_Error (SOSC.EINVAL);
1306 To_Inet_Addr (To_In_Addr (Res), Result);
1314 procedure Initialize (X : in out Sockets_Library_Controller) is
1315 pragma Unreferenced (X);
1318 -- Initialization operation for the GNAT.Sockets package
1320 Empty_Socket_Set.Last := No_Socket;
1321 Reset_Socket_Set (Empty_Socket_Set.Set'Access);
1329 procedure Initialize (Process_Blocking_IO : Boolean) is
1330 Expected : constant Boolean := not SOSC.Thread_Blocking_IO;
1333 if Process_Blocking_IO /= Expected then
1334 raise Socket_Error with
1335 "incorrect Process_Blocking_IO setting, expected " & Expected'Img;
1338 -- This is a dummy placeholder for an obsolete API
1340 -- Real initialization actions are in Initialize primitive operation
1341 -- of Sockets_Library_Controller.
1350 procedure Initialize is
1352 -- This is a dummy placeholder for an obsolete API
1354 -- Real initialization actions are in Initialize primitive operation
1355 -- of Sockets_Library_Controller.
1364 function Is_Empty (Item : Socket_Set_Type) return Boolean is
1366 return Item.Last = No_Socket;
1373 function Is_IP_Address (Name : String) return Boolean is
1375 for J in Name'Range loop
1377 and then Name (J) not in '0' .. '9'
1391 (Item : Socket_Set_Type;
1392 Socket : Socket_Type) return Boolean
1395 return Item.Last /= No_Socket
1396 and then Socket <= Item.Last
1397 and then Is_Socket_In_Set (Item.Set'Access, C.int (Socket)) /= 0;
1404 procedure Listen_Socket
1405 (Socket : Socket_Type;
1406 Length : Natural := 15)
1408 Res : constant C.int := C_Listen (C.int (Socket), C.int (Length));
1410 if Res = Failure then
1411 Raise_Socket_Error (Socket_Errno);
1419 procedure Narrow (Item : in out Socket_Set_Type) is
1420 Last : aliased C.int := C.int (Item.Last);
1422 if Item.Last /= No_Socket then
1423 Last_Socket_In_Set (Item.Set'Access, Last'Unchecked_Access);
1424 Item.Last := Socket_Type (Last);
1432 function Official_Name (E : Host_Entry_Type) return String is
1434 return To_String (E.Official);
1441 function Official_Name (S : Service_Entry_Type) return String is
1443 return To_String (S.Official);
1446 --------------------
1447 -- Wait_On_Socket --
1448 --------------------
1450 procedure Wait_On_Socket
1451 (Socket : Socket_Type;
1453 Timeout : Selector_Duration;
1454 Selector : access Selector_Type := null;
1455 Status : out Selector_Status)
1457 type Local_Selector_Access is access Selector_Type;
1458 for Local_Selector_Access'Storage_Size use Selector_Type'Size;
1460 S : Selector_Access;
1461 -- Selector to use for waiting
1463 R_Fd_Set : Socket_Set_Type;
1464 W_Fd_Set : Socket_Set_Type;
1465 -- Socket sets, empty at elaboration
1468 -- Create selector if not provided by the user
1470 if Selector = null then
1472 Local_S : constant Local_Selector_Access := new Selector_Type;
1474 S := Local_S.all'Unchecked_Access;
1475 Create_Selector (S.all);
1479 S := Selector.all'Access;
1483 Set (R_Fd_Set, Socket);
1485 Set (W_Fd_Set, Socket);
1488 Check_Selector (S.all, R_Fd_Set, W_Fd_Set, Status, Timeout);
1490 -- Cleanup actions (required in all cases to avoid memory leaks)
1498 if Selector = null then
1499 Close_Selector (S.all);
1507 function Port_Number (S : Service_Entry_Type) return Port_Type is
1516 function Protocol_Name (S : Service_Entry_Type) return String is
1518 return To_String (S.Protocol);
1521 ----------------------
1522 -- Raise_Host_Error --
1523 ----------------------
1525 procedure Raise_Host_Error (H_Error : Integer) is
1527 raise Host_Error with
1528 Err_Code_Image (H_Error)
1529 & C.Strings.Value (Host_Error_Messages.Host_Error_Message (H_Error));
1530 end Raise_Host_Error;
1532 ------------------------
1533 -- Raise_Socket_Error --
1534 ------------------------
1536 procedure Raise_Socket_Error (Error : Integer) is
1537 use type C.Strings.chars_ptr;
1539 raise Socket_Error with
1540 Err_Code_Image (Error)
1541 & C.Strings.Value (Socket_Error_Message (Error));
1542 end Raise_Socket_Error;
1549 (Stream : in out Datagram_Socket_Stream_Type;
1550 Item : out Ada.Streams.Stream_Element_Array;
1551 Last : out Ada.Streams.Stream_Element_Offset)
1553 First : Ada.Streams.Stream_Element_Offset := Item'First;
1554 Index : Ada.Streams.Stream_Element_Offset := First - 1;
1555 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
1561 Item (First .. Max),
1567 -- Exit when all or zero data received. Zero means that the socket
1570 exit when Index < First or else Index = Max;
1581 (Stream : in out Stream_Socket_Stream_Type;
1582 Item : out Ada.Streams.Stream_Element_Array;
1583 Last : out Ada.Streams.Stream_Element_Offset)
1585 pragma Warnings (Off, Stream);
1587 First : Ada.Streams.Stream_Element_Offset := Item'First;
1588 Index : Ada.Streams.Stream_Element_Offset := First - 1;
1589 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
1593 Receive_Socket (Stream.Socket, Item (First .. Max), Index);
1596 -- Exit when all or zero data received. Zero means that the socket
1599 exit when Index < First or else Index = Max;
1605 --------------------
1606 -- Receive_Socket --
1607 --------------------
1609 procedure Receive_Socket
1610 (Socket : Socket_Type;
1611 Item : out Ada.Streams.Stream_Element_Array;
1612 Last : out Ada.Streams.Stream_Element_Offset;
1613 Flags : Request_Flag_Type := No_Request_Flag)
1619 C_Recv (C.int (Socket), Item'Address, Item'Length, To_Int (Flags));
1621 if Res = Failure then
1622 Raise_Socket_Error (Socket_Errno);
1625 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1628 --------------------
1629 -- Receive_Socket --
1630 --------------------
1632 procedure Receive_Socket
1633 (Socket : Socket_Type;
1634 Item : out Ada.Streams.Stream_Element_Array;
1635 Last : out Ada.Streams.Stream_Element_Offset;
1636 From : out Sock_Addr_Type;
1637 Flags : Request_Flag_Type := No_Request_Flag)
1640 Sin : aliased Sockaddr_In;
1641 Len : aliased C.int := Sin'Size / 8;
1650 Sin'Unchecked_Access,
1653 if Res = Failure then
1654 Raise_Socket_Error (Socket_Errno);
1657 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1659 To_Inet_Addr (Sin.Sin_Addr, From.Addr);
1660 From.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
1667 function Resolve_Error
1668 (Error_Value : Integer;
1669 From_Errno : Boolean := True) return Error_Type
1671 use GNAT.Sockets.SOSC;
1674 if not From_Errno then
1676 when SOSC.HOST_NOT_FOUND => return Unknown_Host;
1677 when SOSC.TRY_AGAIN => return Host_Name_Lookup_Failure;
1678 when SOSC.NO_RECOVERY => return Non_Recoverable_Error;
1679 when SOSC.NO_DATA => return Unknown_Server_Error;
1680 when others => return Cannot_Resolve_Error;
1684 -- Special case: EAGAIN may be the same value as EWOULDBLOCK, so we
1685 -- can't include it in the case statement below.
1687 pragma Warnings (Off);
1688 -- Condition "EAGAIN /= EWOULDBLOCK" is known at compile time
1690 if EAGAIN /= EWOULDBLOCK and then Error_Value = EAGAIN then
1691 return Resource_Temporarily_Unavailable;
1694 pragma Warnings (On);
1697 when ENOERROR => return Success;
1698 when EACCES => return Permission_Denied;
1699 when EADDRINUSE => return Address_Already_In_Use;
1700 when EADDRNOTAVAIL => return Cannot_Assign_Requested_Address;
1701 when EAFNOSUPPORT => return
1702 Address_Family_Not_Supported_By_Protocol;
1703 when EALREADY => return Operation_Already_In_Progress;
1704 when EBADF => return Bad_File_Descriptor;
1705 when ECONNABORTED => return Software_Caused_Connection_Abort;
1706 when ECONNREFUSED => return Connection_Refused;
1707 when ECONNRESET => return Connection_Reset_By_Peer;
1708 when EDESTADDRREQ => return Destination_Address_Required;
1709 when EFAULT => return Bad_Address;
1710 when EHOSTDOWN => return Host_Is_Down;
1711 when EHOSTUNREACH => return No_Route_To_Host;
1712 when EINPROGRESS => return Operation_Now_In_Progress;
1713 when EINTR => return Interrupted_System_Call;
1714 when EINVAL => return Invalid_Argument;
1715 when EIO => return Input_Output_Error;
1716 when EISCONN => return Transport_Endpoint_Already_Connected;
1717 when ELOOP => return Too_Many_Symbolic_Links;
1718 when EMFILE => return Too_Many_Open_Files;
1719 when EMSGSIZE => return Message_Too_Long;
1720 when ENAMETOOLONG => return File_Name_Too_Long;
1721 when ENETDOWN => return Network_Is_Down;
1722 when ENETRESET => return
1723 Network_Dropped_Connection_Because_Of_Reset;
1724 when ENETUNREACH => return Network_Is_Unreachable;
1725 when ENOBUFS => return No_Buffer_Space_Available;
1726 when ENOPROTOOPT => return Protocol_Not_Available;
1727 when ENOTCONN => return Transport_Endpoint_Not_Connected;
1728 when ENOTSOCK => return Socket_Operation_On_Non_Socket;
1729 when EOPNOTSUPP => return Operation_Not_Supported;
1730 when EPFNOSUPPORT => return Protocol_Family_Not_Supported;
1731 when EPIPE => return Broken_Pipe;
1732 when EPROTONOSUPPORT => return Protocol_Not_Supported;
1733 when EPROTOTYPE => return Protocol_Wrong_Type_For_Socket;
1734 when ESHUTDOWN => return
1735 Cannot_Send_After_Transport_Endpoint_Shutdown;
1736 when ESOCKTNOSUPPORT => return Socket_Type_Not_Supported;
1737 when ETIMEDOUT => return Connection_Timed_Out;
1738 when ETOOMANYREFS => return Too_Many_References;
1739 when EWOULDBLOCK => return Resource_Temporarily_Unavailable;
1741 when others => return Cannot_Resolve_Error;
1745 -----------------------
1746 -- Resolve_Exception --
1747 -----------------------
1749 function Resolve_Exception
1750 (Occurrence : Exception_Occurrence) return Error_Type
1752 Id : constant Exception_Id := Exception_Identity (Occurrence);
1753 Msg : constant String := Exception_Message (Occurrence);
1760 while First <= Msg'Last
1761 and then Msg (First) not in '0' .. '9'
1766 if First > Msg'Last then
1767 return Cannot_Resolve_Error;
1771 while Last < Msg'Last
1772 and then Msg (Last + 1) in '0' .. '9'
1777 Val := Integer'Value (Msg (First .. Last));
1779 if Id = Socket_Error_Id then
1780 return Resolve_Error (Val);
1781 elsif Id = Host_Error_Id then
1782 return Resolve_Error (Val, False);
1784 return Cannot_Resolve_Error;
1786 end Resolve_Exception;
1788 --------------------
1789 -- Receive_Vector --
1790 --------------------
1792 procedure Receive_Vector
1793 (Socket : Socket_Type;
1794 Vector : Vector_Type;
1795 Count : out Ada.Streams.Stream_Element_Count)
1806 if Res = Failure then
1807 Raise_Socket_Error (Socket_Errno);
1810 Count := Ada.Streams.Stream_Element_Count (Res);
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 : Sockaddr_In_Access;
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'Unchecked_Access;
1868 Len := Sin'Size / 8;
1879 Set_Forced_Flags (To_Int (Flags)),
1883 if Res = Failure then
1884 Raise_Socket_Error (Socket_Errno);
1887 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1894 procedure Send_Vector
1895 (Socket : Socket_Type;
1896 Vector : Vector_Type;
1897 Count : out Ada.Streams.Stream_Element_Count)
1901 This_Iov_Count : C.int;
1906 while Iov_Count < Vector'Length loop
1908 pragma Warnings (Off);
1909 -- Following test may be compile time known on some targets
1911 if Vector'Length - Iov_Count > SOSC.IOV_MAX then
1912 This_Iov_Count := SOSC.IOV_MAX;
1914 This_Iov_Count := Vector'Length - Iov_Count;
1917 pragma Warnings (On);
1922 Vector (Vector'First + Integer (Iov_Count))'Address,
1925 if Res = Failure then
1926 Raise_Socket_Error (Socket_Errno);
1929 Count := Count + Ada.Streams.Stream_Element_Count (Res);
1930 Iov_Count := Iov_Count + This_Iov_Count;
1938 procedure Set (Item : in out Socket_Set_Type; Socket : Socket_Type) is
1940 if Item.Last = No_Socket then
1942 -- Uninitialized socket set, make sure it is properly zeroed out
1944 Reset_Socket_Set (Item.Set'Access);
1945 Item.Last := Socket;
1947 elsif Item.Last < Socket then
1948 Item.Last := Socket;
1951 Insert_Socket_In_Set (Item.Set'Access, C.int (Socket));
1954 ----------------------
1955 -- Set_Forced_Flags --
1956 ----------------------
1958 function Set_Forced_Flags (F : C.int) return C.int is
1959 use type C.unsigned;
1960 function To_unsigned is
1961 new Ada.Unchecked_Conversion (C.int, C.unsigned);
1963 new Ada.Unchecked_Conversion (C.unsigned, C.int);
1965 return To_int (To_unsigned (F) or SOSC.MSG_Forced_Flags);
1966 end Set_Forced_Flags;
1968 -----------------------
1969 -- Set_Socket_Option --
1970 -----------------------
1972 procedure Set_Socket_Option
1973 (Socket : Socket_Type;
1974 Level : Level_Type := Socket_Level;
1975 Option : Option_Type)
1977 V8 : aliased Two_Ints;
1979 V1 : aliased C.unsigned_char;
1980 VT : aliased Timeval;
1982 Add : System.Address := Null_Address;
1991 V4 := C.int (Boolean'Pos (Option.Enabled));
1996 V8 (V8'First) := C.int (Boolean'Pos (Option.Enabled));
1997 V8 (V8'Last) := C.int (Option.Seconds);
2003 V4 := C.int (Option.Size);
2008 V4 := C.int (Boolean'Pos (True));
2012 when Add_Membership |
2014 V8 (V8'First) := To_Int (To_In_Addr (Option.Multicast_Address));
2015 V8 (V8'Last) := To_Int (To_In_Addr (Option.Local_Interface));
2019 when Multicast_If =>
2020 V4 := To_Int (To_In_Addr (Option.Outgoing_If));
2024 when Multicast_TTL =>
2025 V1 := C.unsigned_char (Option.Time_To_Live);
2029 when Multicast_Loop |
2030 Receive_Packet_Info =>
2031 V1 := C.unsigned_char (Boolean'Pos (Option.Enabled));
2037 VT := To_Timeval (Option.Timeout);
2046 Options (Option.Name),
2049 if Res = Failure then
2050 Raise_Socket_Error (Socket_Errno);
2052 end Set_Socket_Option;
2054 ----------------------
2055 -- Short_To_Network --
2056 ----------------------
2058 function Short_To_Network (S : C.unsigned_short) return C.unsigned_short is
2059 use type C.unsigned_short;
2062 -- Big-endian case. No conversion needed. On these platforms,
2063 -- htons() defaults to a null procedure.
2065 pragma Warnings (Off);
2066 -- Since the test can generate "always True/False" warning
2068 if Default_Bit_Order = High_Order_First then
2071 pragma Warnings (On);
2073 -- Little-endian case. We must swap the high and low bytes of this
2074 -- short to make the port number network compliant.
2077 return (S / 256) + (S mod 256) * 256;
2079 end Short_To_Network;
2081 ---------------------
2082 -- Shutdown_Socket --
2083 ---------------------
2085 procedure Shutdown_Socket
2086 (Socket : Socket_Type;
2087 How : Shutmode_Type := Shut_Read_Write)
2092 Res := C_Shutdown (C.int (Socket), Shutmodes (How));
2094 if Res = Failure then
2095 Raise_Socket_Error (Socket_Errno);
2097 end Shutdown_Socket;
2104 (Socket : Socket_Type;
2105 Send_To : Sock_Addr_Type) return Stream_Access
2107 S : Datagram_Socket_Stream_Access;
2110 S := new Datagram_Socket_Stream_Type;
2113 S.From := Get_Socket_Name (Socket);
2114 return Stream_Access (S);
2121 function Stream (Socket : Socket_Type) return Stream_Access is
2122 S : Stream_Socket_Stream_Access;
2124 S := new Stream_Socket_Stream_Type;
2126 return Stream_Access (S);
2133 procedure Stream_Write
2134 (Socket : Socket_Type;
2135 Item : Ada.Streams.Stream_Element_Array;
2136 To : access Sock_Addr_Type)
2138 First : Ada.Streams.Stream_Element_Offset;
2139 Index : Ada.Streams.Stream_Element_Offset;
2140 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
2143 First := Item'First;
2145 while First <= Max loop
2146 Send_Socket (Socket, Item (First .. Max), Index, To);
2148 -- Exit when all or zero data sent. Zero means that the socket has
2149 -- been closed by peer.
2151 exit when Index < First or else Index = Max;
2156 -- For an empty array, we have First > Max, and hence Index >= Max (no
2157 -- error, the loop above is never executed). After a succesful send,
2158 -- Index = Max. The only remaining case, Index < Max, is therefore
2159 -- always an actual send failure.
2162 Raise_Socket_Error (Socket_Errno);
2170 function To_C (Socket : Socket_Type) return Integer is
2172 return Integer (Socket);
2179 function To_Duration (Val : Timeval) return Timeval_Duration is
2181 return Natural (Val.Tv_Sec) * 1.0 + Natural (Val.Tv_Usec) * 1.0E-6;
2188 function To_Host_Entry (E : Hostent) return Host_Entry_Type is
2191 Official : constant String :=
2192 C.Strings.Value (E.H_Name);
2194 Aliases : constant Chars_Ptr_Array :=
2195 Chars_Ptr_Pointers.Value (E.H_Aliases);
2196 -- H_Aliases points to a list of name aliases. The list is terminated by
2199 Addresses : constant In_Addr_Access_Array :=
2200 In_Addr_Access_Pointers.Value (E.H_Addr_List);
2201 -- H_Addr_List points to a list of binary addresses (in network byte
2202 -- order). The list is terminated by a NULL pointer.
2204 -- H_Length is not used because it is currently only set to 4.
2205 -- H_Addrtype is always AF_INET
2207 Result : Host_Entry_Type
2208 (Aliases_Length => Aliases'Length - 1,
2209 Addresses_Length => Addresses'Length - 1);
2210 -- The last element is a null pointer
2216 Result.Official := To_Name (Official);
2218 Source := Aliases'First;
2219 Target := Result.Aliases'First;
2220 while Target <= Result.Aliases_Length loop
2221 Result.Aliases (Target) :=
2222 To_Name (C.Strings.Value (Aliases (Source)));
2223 Source := Source + 1;
2224 Target := Target + 1;
2227 Source := Addresses'First;
2228 Target := Result.Addresses'First;
2229 while Target <= Result.Addresses_Length loop
2230 To_Inet_Addr (Addresses (Source).all, Result.Addresses (Target));
2231 Source := Source + 1;
2232 Target := Target + 1;
2242 function To_In_Addr (Addr : Inet_Addr_Type) return In_Addr is
2244 if Addr.Family = Family_Inet then
2245 return (S_B1 => C.unsigned_char (Addr.Sin_V4 (1)),
2246 S_B2 => C.unsigned_char (Addr.Sin_V4 (2)),
2247 S_B3 => C.unsigned_char (Addr.Sin_V4 (3)),
2248 S_B4 => C.unsigned_char (Addr.Sin_V4 (4)));
2251 raise Socket_Error with "IPv6 not supported";
2258 procedure To_Inet_Addr
2260 Result : out Inet_Addr_Type) is
2262 Result.Sin_V4 (1) := Inet_Addr_Comp_Type (Addr.S_B1);
2263 Result.Sin_V4 (2) := Inet_Addr_Comp_Type (Addr.S_B2);
2264 Result.Sin_V4 (3) := Inet_Addr_Comp_Type (Addr.S_B3);
2265 Result.Sin_V4 (4) := Inet_Addr_Comp_Type (Addr.S_B4);
2272 function To_Int (F : Request_Flag_Type) return C.int
2274 Current : Request_Flag_Type := F;
2275 Result : C.int := 0;
2278 for J in Flags'Range loop
2279 exit when Current = 0;
2281 if Current mod 2 /= 0 then
2282 if Flags (J) = -1 then
2283 Raise_Socket_Error (SOSC.EOPNOTSUPP);
2286 Result := Result + Flags (J);
2289 Current := Current / 2;
2299 function To_Name (N : String) return Name_Type is
2301 return Name_Type'(N'Length, N);
2304 ----------------------
2305 -- To_Service_Entry --
2306 ----------------------
2308 function To_Service_Entry (E : Servent) return Service_Entry_Type is
2311 Official : constant String := C.Strings.Value (E.S_Name);
2313 Aliases : constant Chars_Ptr_Array :=
2314 Chars_Ptr_Pointers.Value (E.S_Aliases);
2315 -- S_Aliases points to a list of name aliases. The list is
2316 -- terminated by a NULL pointer.
2318 Protocol : constant String := C.Strings.Value (E.S_Proto);
2320 Result : Service_Entry_Type (Aliases_Length => Aliases'Length - 1);
2321 -- The last element is a null pointer
2327 Result.Official := To_Name (Official);
2329 Source := Aliases'First;
2330 Target := Result.Aliases'First;
2331 while Target <= Result.Aliases_Length loop
2332 Result.Aliases (Target) :=
2333 To_Name (C.Strings.Value (Aliases (Source)));
2334 Source := Source + 1;
2335 Target := Target + 1;
2339 Port_Type (Network_To_Short (C.unsigned_short (E.S_Port)));
2341 Result.Protocol := To_Name (Protocol);
2343 end To_Service_Entry;
2349 function To_String (HN : Name_Type) return String is
2351 return HN.Name (1 .. HN.Length);
2358 function To_Timeval (Val : Timeval_Duration) return Timeval is
2363 -- If zero, set result as zero (otherwise it gets rounded down to -1)
2369 -- Normal case where we do round down
2372 S := time_t (Val - 0.5);
2373 uS := suseconds_t (1_000_000 * (Val - Selector_Duration (S)));
2384 (Stream : in out Datagram_Socket_Stream_Type;
2385 Item : Ada.Streams.Stream_Element_Array)
2388 Stream_Write (Stream.Socket, Item, To => Stream.To'Unrestricted_Access);
2396 (Stream : in out Stream_Socket_Stream_Type;
2397 Item : Ada.Streams.Stream_Element_Array)
2400 Stream_Write (Stream.Socket, Item, To => null);
2403 Sockets_Library_Controller_Object : Sockets_Library_Controller;
2404 pragma Unreferenced (Sockets_Library_Controller_Object);
2405 -- The elaboration and finalization of this object perform the required
2406 -- initialization and cleanup actions for the sockets library.