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.Unchecked_Conversion;
38 with Interfaces.C.Strings;
40 with GNAT.Sockets.Thin_Common; use GNAT.Sockets.Thin_Common;
41 with GNAT.Sockets.Thin; use GNAT.Sockets.Thin;
42 with GNAT.Sockets.Thin.Task_Safe_NetDB; use GNAT.Sockets.Thin.Task_Safe_NetDB;
44 with GNAT.Sockets.Linker_Options;
45 pragma Warnings (Off, GNAT.Sockets.Linker_Options);
46 -- Need to include pragma Linker_Options which is platform dependent
48 with System; use System;
50 package body GNAT.Sockets is
52 package C renames Interfaces.C;
56 Finalized : Boolean := False;
57 Initialized : Boolean := False;
59 ENOERROR : constant := 0;
61 Netdb_Buffer_Size : constant := Constants.Need_Netdb_Buffer * 1024;
62 -- The network database functions gethostbyname, gethostbyaddr,
63 -- getservbyname and getservbyport can either be guaranteed task safe by
64 -- the operating system, or else return data through a user-provided buffer
65 -- to ensure concurrent uses do not interfere.
67 -- Correspondence tables
69 Levels : constant array (Level_Type) of C.int :=
70 (Socket_Level => Constants.SOL_SOCKET,
71 IP_Protocol_For_IP_Level => Constants.IPPROTO_IP,
72 IP_Protocol_For_UDP_Level => Constants.IPPROTO_UDP,
73 IP_Protocol_For_TCP_Level => Constants.IPPROTO_TCP);
75 Modes : constant array (Mode_Type) of C.int :=
76 (Socket_Stream => Constants.SOCK_STREAM,
77 Socket_Datagram => Constants.SOCK_DGRAM);
79 Shutmodes : constant array (Shutmode_Type) of C.int :=
80 (Shut_Read => Constants.SHUT_RD,
81 Shut_Write => Constants.SHUT_WR,
82 Shut_Read_Write => Constants.SHUT_RDWR);
84 Requests : constant array (Request_Name) of C.int :=
85 (Non_Blocking_IO => Constants.FIONBIO,
86 N_Bytes_To_Read => Constants.FIONREAD);
88 Options : constant array (Option_Name) of C.int :=
89 (Keep_Alive => Constants.SO_KEEPALIVE,
90 Reuse_Address => Constants.SO_REUSEADDR,
91 Broadcast => Constants.SO_BROADCAST,
92 Send_Buffer => Constants.SO_SNDBUF,
93 Receive_Buffer => Constants.SO_RCVBUF,
94 Linger => Constants.SO_LINGER,
95 Error => Constants.SO_ERROR,
96 No_Delay => Constants.TCP_NODELAY,
97 Add_Membership => Constants.IP_ADD_MEMBERSHIP,
98 Drop_Membership => Constants.IP_DROP_MEMBERSHIP,
99 Multicast_If => Constants.IP_MULTICAST_IF,
100 Multicast_TTL => Constants.IP_MULTICAST_TTL,
101 Multicast_Loop => Constants.IP_MULTICAST_LOOP,
102 Receive_Packet_Info => Constants.IP_PKTINFO,
103 Send_Timeout => Constants.SO_SNDTIMEO,
104 Receive_Timeout => Constants.SO_RCVTIMEO);
105 -- ??? Note: for OpenSolaris, Receive_Packet_Info should be IP_RECVPKTINFO,
106 -- but for Linux compatibility this constant is the same as IP_PKTINFO.
108 Flags : constant array (0 .. 3) of C.int :=
109 (0 => Constants.MSG_OOB, -- Process_Out_Of_Band_Data
110 1 => Constants.MSG_PEEK, -- Peek_At_Incoming_Data
111 2 => Constants.MSG_WAITALL, -- Wait_For_A_Full_Reception
112 3 => Constants.MSG_EOR); -- Send_End_Of_Record
114 Socket_Error_Id : constant Exception_Id := Socket_Error'Identity;
115 Host_Error_Id : constant Exception_Id := Host_Error'Identity;
117 Hex_To_Char : constant String (1 .. 16) := "0123456789ABCDEF";
118 -- Use to print in hexadecimal format
120 function Err_Code_Image (E : Integer) return String;
121 -- Return the value of E surrounded with brackets
123 -----------------------
124 -- Local subprograms --
125 -----------------------
127 function Resolve_Error
128 (Error_Value : Integer;
129 From_Errno : Boolean := True) return Error_Type;
130 -- Associate an enumeration value (error_type) to en error value (errno).
131 -- From_Errno prevents from mixing h_errno with errno.
133 function To_Name (N : String) return Name_Type;
134 function To_String (HN : Name_Type) return String;
135 -- Conversion functions
137 function To_Int (F : Request_Flag_Type) return C.int;
138 -- Return the int value corresponding to the specified flags combination
140 function Set_Forced_Flags (F : C.int) return C.int;
141 -- Return F with the bits from Constants.MSG_Forced_Flags forced set
143 function Short_To_Network
144 (S : C.unsigned_short) return C.unsigned_short;
145 pragma Inline (Short_To_Network);
146 -- Convert a port number into a network port number
148 function Network_To_Short
149 (S : C.unsigned_short) return C.unsigned_short
150 renames Short_To_Network;
151 -- Symmetric operation
154 (Val : Inet_Addr_VN_Type;
155 Hex : Boolean := False) return String;
156 -- Output an array of inet address components in hex or decimal mode
158 function Is_IP_Address (Name : String) return Boolean;
159 -- Return true when Name is an IP address in standard dot notation
161 function To_In_Addr (Addr : Inet_Addr_Type) return In_Addr;
162 procedure To_Inet_Addr
164 Result : out Inet_Addr_Type);
165 -- Conversion functions
167 function To_Host_Entry (E : Hostent) return Host_Entry_Type;
168 -- Conversion function
170 function To_Service_Entry (E : Servent) return Service_Entry_Type;
171 -- Conversion function
173 function To_Timeval (Val : Timeval_Duration) return Timeval;
174 -- Separate Val in seconds and microseconds
176 function To_Duration (Val : Timeval) return Timeval_Duration;
177 -- Reconstruct a Duration value from a Timeval record (seconds and
180 procedure Raise_Socket_Error (Error : Integer);
181 -- Raise Socket_Error with an exception message describing the error code
184 procedure Raise_Host_Error (H_Error : Integer);
185 -- Raise Host_Error exception with message describing error code (note
186 -- hstrerror seems to be obsolete) from h_errno.
188 procedure Narrow (Item : in out Socket_Set_Type);
189 -- Update Last as it may be greater than the real last socket
191 -- Types needed for Datagram_Socket_Stream_Type
193 type Datagram_Socket_Stream_Type is new Root_Stream_Type with record
194 Socket : Socket_Type;
196 From : Sock_Addr_Type;
199 type Datagram_Socket_Stream_Access is
200 access all Datagram_Socket_Stream_Type;
203 (Stream : in out Datagram_Socket_Stream_Type;
204 Item : out Ada.Streams.Stream_Element_Array;
205 Last : out Ada.Streams.Stream_Element_Offset);
208 (Stream : in out Datagram_Socket_Stream_Type;
209 Item : Ada.Streams.Stream_Element_Array);
211 -- Types needed for Stream_Socket_Stream_Type
213 type Stream_Socket_Stream_Type is new Root_Stream_Type with record
214 Socket : Socket_Type;
217 type Stream_Socket_Stream_Access is
218 access all Stream_Socket_Stream_Type;
221 (Stream : in out Stream_Socket_Stream_Type;
222 Item : out Ada.Streams.Stream_Element_Array;
223 Last : out Ada.Streams.Stream_Element_Offset);
226 (Stream : in out Stream_Socket_Stream_Type;
227 Item : Ada.Streams.Stream_Element_Array);
229 procedure Wait_On_Socket
230 (Socket : Socket_Type;
232 Timeout : Selector_Duration;
233 Selector : access Selector_Type := null;
234 Status : out Selector_Status);
235 -- Common code for variants of socket operations supporting a timeout:
236 -- block in Check_Selector on Socket for at most the indicated timeout.
237 -- If For_Read is True, Socket is added to the read set for this call, else
238 -- it is added to the write set. If no selector is provided, a local one is
239 -- created for this call and destroyed prior to returning.
245 function "+" (L, R : Request_Flag_Type) return Request_Flag_Type is
254 procedure Abort_Selector (Selector : Selector_Type) is
258 -- Send one byte to unblock select system call
260 Res := Signalling_Fds.Write (C.int (Selector.W_Sig_Socket));
262 if Res = Failure then
263 Raise_Socket_Error (Socket_Errno);
271 procedure Accept_Socket
272 (Server : Socket_Type;
273 Socket : out Socket_Type;
274 Address : out Sock_Addr_Type)
277 Sin : aliased Sockaddr_In;
278 Len : aliased C.int := Sin'Size / 8;
281 Res := C_Accept (C.int (Server), Sin'Address, Len'Access);
283 if Res = Failure then
284 Raise_Socket_Error (Socket_Errno);
287 Socket := Socket_Type (Res);
289 To_Inet_Addr (Sin.Sin_Addr, Address.Addr);
290 Address.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
297 procedure Accept_Socket
298 (Server : Socket_Type;
299 Socket : out Socket_Type;
300 Address : out Sock_Addr_Type;
301 Timeout : Selector_Duration;
302 Selector : access Selector_Type := null;
303 Status : out Selector_Status)
306 -- Wait for socket to become available for reading
312 Selector => Selector,
315 -- Accept connection if available
317 if Status = Completed then
318 Accept_Socket (Server, Socket, Address);
329 (E : Host_Entry_Type;
330 N : Positive := 1) return Inet_Addr_Type
333 return E.Addresses (N);
336 ----------------------
337 -- Addresses_Length --
338 ----------------------
340 function Addresses_Length (E : Host_Entry_Type) return Natural is
342 return E.Addresses_Length;
343 end Addresses_Length;
350 (E : Host_Entry_Type;
351 N : Positive := 1) return String
354 return To_String (E.Aliases (N));
362 (S : Service_Entry_Type;
363 N : Positive := 1) return String
366 return To_String (S.Aliases (N));
373 function Aliases_Length (E : Host_Entry_Type) return Natural is
375 return E.Aliases_Length;
382 function Aliases_Length (S : Service_Entry_Type) return Natural is
384 return S.Aliases_Length;
391 procedure Bind_Socket
392 (Socket : Socket_Type;
393 Address : Sock_Addr_Type)
396 Sin : aliased Sockaddr_In;
397 Len : constant C.int := Sin'Size / 8;
398 -- This assumes that Address.Family = Family_Inet???
401 if Address.Family = Family_Inet6 then
402 raise Socket_Error with "IPv6 not supported";
405 Set_Family (Sin.Sin_Family, Address.Family);
406 Set_Address (Sin'Unchecked_Access, To_In_Addr (Address.Addr));
408 (Sin'Unchecked_Access,
409 Short_To_Network (C.unsigned_short (Address.Port)));
411 Res := C_Bind (C.int (Socket), Sin'Address, Len);
413 if Res = Failure then
414 Raise_Socket_Error (Socket_Errno);
422 procedure Check_Selector
423 (Selector : in out Selector_Type;
424 R_Socket_Set : in out Socket_Set_Type;
425 W_Socket_Set : in out Socket_Set_Type;
426 Status : out Selector_Status;
427 Timeout : Selector_Duration := Forever)
429 E_Socket_Set : Socket_Set_Type; -- (No_Socket, No_Fd_Set_Access)
432 (Selector, R_Socket_Set, W_Socket_Set, E_Socket_Set, Status, Timeout);
439 procedure Check_Selector
440 (Selector : in out Selector_Type;
441 R_Socket_Set : in out Socket_Set_Type;
442 W_Socket_Set : in out Socket_Set_Type;
443 E_Socket_Set : in out Socket_Set_Type;
444 Status : out Selector_Status;
445 Timeout : Selector_Duration := Forever)
449 RSig : Socket_Type renames Selector.R_Sig_Socket;
450 RSet : Socket_Set_Type;
451 WSet : Socket_Set_Type;
452 ESet : Socket_Set_Type;
453 TVal : aliased Timeval;
454 TPtr : Timeval_Access;
460 -- No timeout or Forever is indicated by a null timeval pointer
462 if Timeout = Forever then
465 TVal := To_Timeval (Timeout);
466 TPtr := TVal'Unchecked_Access;
469 -- Copy R_Socket_Set in RSet and add read signalling socket
471 RSet := (Set => New_Socket_Set (R_Socket_Set.Set),
472 Last => R_Socket_Set.Last);
475 -- Copy W_Socket_Set in WSet
477 WSet := (Set => New_Socket_Set (W_Socket_Set.Set),
478 Last => W_Socket_Set.Last);
480 -- Copy E_Socket_Set in ESet
482 ESet := (Set => New_Socket_Set (E_Socket_Set.Set),
483 Last => E_Socket_Set.Last);
485 Last := C.int'Max (C.int'Max (C.int (RSet.Last),
497 if Res = Failure then
498 Raise_Socket_Error (Socket_Errno);
501 -- If Select was resumed because of read signalling socket, read this
502 -- data and remove socket from set.
504 if Is_Set (RSet, RSig) then
507 Res := Signalling_Fds.Read (C.int (RSig));
509 if Res = Failure then
510 Raise_Socket_Error (Socket_Errno);
519 -- Update RSet, WSet and ESet in regard to their new socket sets
525 -- Reset RSet as it should be if R_Sig_Socket was not added
527 if Is_Empty (RSet) then
531 if Is_Empty (WSet) then
535 if Is_Empty (ESet) then
539 -- Deliver RSet, WSet and ESet
541 Empty (R_Socket_Set);
542 R_Socket_Set := RSet;
544 Empty (W_Socket_Set);
545 W_Socket_Set := WSet;
547 Empty (E_Socket_Set);
548 E_Socket_Set := ESet;
553 -- The local socket sets must be emptied before propagating
554 -- Socket_Error so the associated storage is freed.
568 (Item : in out Socket_Set_Type;
569 Socket : Socket_Type)
571 Last : aliased C.int := C.int (Item.Last);
573 if Item.Last /= No_Socket then
574 Remove_Socket_From_Set (Item.Set, C.int (Socket));
575 Last_Socket_In_Set (Item.Set, Last'Unchecked_Access);
576 Item.Last := Socket_Type (Last);
584 procedure Close_Selector (Selector : in out Selector_Type) is
586 -- Close the signalling file descriptors used internally for the
587 -- implementation of Abort_Selector.
589 Signalling_Fds.Close (C.int (Selector.R_Sig_Socket));
590 Signalling_Fds.Close (C.int (Selector.W_Sig_Socket));
592 -- Reset R_Sig_Socket and W_Sig_Socket to No_Socket to ensure that any
593 -- (erroneous) subsequent attempt to use this selector properly fails.
595 Selector.R_Sig_Socket := No_Socket;
596 Selector.W_Sig_Socket := No_Socket;
603 procedure Close_Socket (Socket : Socket_Type) is
607 Res := C_Close (C.int (Socket));
609 if Res = Failure then
610 Raise_Socket_Error (Socket_Errno);
618 procedure Connect_Socket
619 (Socket : Socket_Type;
620 Server : Sock_Addr_Type)
623 Sin : aliased Sockaddr_In;
624 Len : constant C.int := Sin'Size / 8;
627 if Server.Family = Family_Inet6 then
628 raise Socket_Error with "IPv6 not supported";
631 Set_Family (Sin.Sin_Family, Server.Family);
632 Set_Address (Sin'Unchecked_Access, To_In_Addr (Server.Addr));
634 (Sin'Unchecked_Access,
635 Short_To_Network (C.unsigned_short (Server.Port)));
637 Res := C_Connect (C.int (Socket), Sin'Address, Len);
639 if Res = Failure then
640 Raise_Socket_Error (Socket_Errno);
648 procedure Connect_Socket
649 (Socket : Socket_Type;
650 Server : Sock_Addr_Type;
651 Timeout : Selector_Duration;
652 Selector : access Selector_Type := null;
653 Status : out Selector_Status)
656 -- Used to set Socket to non-blocking I/O
659 -- Set the socket to non-blocking I/O
661 Req := (Name => Non_Blocking_IO, Enabled => True);
662 Control_Socket (Socket, Request => Req);
664 -- Start operation (non-blocking), will raise Socket_Error with
668 Connect_Socket (Socket, Server);
670 when E : Socket_Error =>
671 if Resolve_Exception (E) = Operation_Now_In_Progress then
678 -- Wait for socket to become available for writing
684 Selector => Selector,
687 -- Reset the socket to blocking I/O
689 Req := (Name => Non_Blocking_IO, Enabled => False);
690 Control_Socket (Socket, Request => Req);
697 procedure Control_Socket
698 (Socket : Socket_Type;
699 Request : in out Request_Type)
706 when Non_Blocking_IO =>
707 Arg := C.int (Boolean'Pos (Request.Enabled));
709 when N_Bytes_To_Read =>
715 Requests (Request.Name),
716 Arg'Unchecked_Access);
718 if Res = Failure then
719 Raise_Socket_Error (Socket_Errno);
723 when Non_Blocking_IO =>
726 when N_Bytes_To_Read =>
727 Request.Size := Natural (Arg);
736 (Source : Socket_Set_Type;
737 Target : in out Socket_Set_Type)
741 if Source.Last /= No_Socket then
742 Target.Set := New_Socket_Set (Source.Set);
743 Target.Last := Source.Last;
747 ---------------------
748 -- Create_Selector --
749 ---------------------
751 procedure Create_Selector (Selector : out Selector_Type) is
752 Two_Fds : aliased Fd_Pair;
756 -- We open two signalling file descriptors. One of them is used to send
757 -- data to the other, which is included in a C_Select socket set. The
758 -- communication is used to force a call to C_Select to complete, and
759 -- the waiting task to resume its execution.
761 Res := Signalling_Fds.Create (Two_Fds'Access);
763 if Res = Failure then
764 Raise_Socket_Error (Socket_Errno);
767 Selector.R_Sig_Socket := Socket_Type (Two_Fds (Read_End));
768 Selector.W_Sig_Socket := Socket_Type (Two_Fds (Write_End));
775 procedure Create_Socket
776 (Socket : out Socket_Type;
777 Family : Family_Type := Family_Inet;
778 Mode : Mode_Type := Socket_Stream)
783 Res := C_Socket (Families (Family), Modes (Mode), 0);
785 if Res = Failure then
786 Raise_Socket_Error (Socket_Errno);
789 Socket := Socket_Type (Res);
796 procedure Empty (Item : in out Socket_Set_Type) is
798 if Item.Set /= No_Fd_Set_Access then
799 Free_Socket_Set (Item.Set);
800 Item.Set := No_Fd_Set_Access;
803 Item.Last := No_Socket;
810 function Err_Code_Image (E : Integer) return String is
811 Msg : String := E'Img & "] ";
813 Msg (Msg'First) := '[';
821 procedure Finalize is
836 (Item : in out Socket_Set_Type;
837 Socket : out Socket_Type)
840 L : aliased C.int := C.int (Item.Last);
843 if Item.Last /= No_Socket then
845 (Item.Set, L'Unchecked_Access, S'Unchecked_Access);
846 Item.Last := Socket_Type (L);
847 Socket := Socket_Type (S);
858 (Stream : not null Stream_Access) return Sock_Addr_Type
861 if Stream.all in Datagram_Socket_Stream_Type then
862 return Datagram_Socket_Stream_Type (Stream.all).From;
864 return Get_Peer_Name (Stream_Socket_Stream_Type (Stream.all).Socket);
868 -------------------------
869 -- Get_Host_By_Address --
870 -------------------------
872 function Get_Host_By_Address
873 (Address : Inet_Addr_Type;
874 Family : Family_Type := Family_Inet) return Host_Entry_Type
876 pragma Unreferenced (Family);
878 HA : aliased In_Addr := To_In_Addr (Address);
879 Buflen : constant C.int := Netdb_Buffer_Size;
880 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
881 Res : aliased Hostent;
885 if Safe_Gethostbyaddr (HA'Address, HA'Size / 8, Constants.AF_INET,
886 Res'Access, Buf'Address, Buflen, Err'Access) /= 0
888 Raise_Host_Error (Integer (Err));
891 return To_Host_Entry (Res);
892 end Get_Host_By_Address;
894 ----------------------
895 -- Get_Host_By_Name --
896 ----------------------
898 function Get_Host_By_Name (Name : String) return Host_Entry_Type is
900 -- Detect IP address name and redirect to Inet_Addr
902 if Is_IP_Address (Name) then
903 return Get_Host_By_Address (Inet_Addr (Name));
907 HN : constant C.char_array := C.To_C (Name);
908 Buflen : constant C.int := Netdb_Buffer_Size;
909 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
910 Res : aliased Hostent;
914 if Safe_Gethostbyname
915 (HN, Res'Access, Buf'Address, Buflen, Err'Access) /= 0
917 Raise_Host_Error (Integer (Err));
920 return To_Host_Entry (Res);
922 end Get_Host_By_Name;
928 function Get_Peer_Name (Socket : Socket_Type) return Sock_Addr_Type is
929 Sin : aliased Sockaddr_In;
930 Len : aliased C.int := Sin'Size / 8;
931 Res : Sock_Addr_Type (Family_Inet);
934 if C_Getpeername (C.int (Socket), Sin'Address, Len'Access) = Failure then
935 Raise_Socket_Error (Socket_Errno);
938 To_Inet_Addr (Sin.Sin_Addr, Res.Addr);
939 Res.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
944 -------------------------
945 -- Get_Service_By_Name --
946 -------------------------
948 function Get_Service_By_Name
950 Protocol : String) return Service_Entry_Type
952 SN : constant C.char_array := C.To_C (Name);
953 SP : constant C.char_array := C.To_C (Protocol);
954 Buflen : constant C.int := Netdb_Buffer_Size;
955 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
956 Res : aliased Servent;
959 if Safe_Getservbyname (SN, SP, Res'Access, Buf'Address, Buflen) /= 0 then
960 raise Service_Error with "Service not found";
963 -- Translate from the C format to the API format
965 return To_Service_Entry (Res);
966 end Get_Service_By_Name;
968 -------------------------
969 -- Get_Service_By_Port --
970 -------------------------
972 function Get_Service_By_Port
974 Protocol : String) return Service_Entry_Type
976 SP : constant C.char_array := C.To_C (Protocol);
977 Buflen : constant C.int := Netdb_Buffer_Size;
978 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
979 Res : aliased Servent;
982 if Safe_Getservbyport
983 (C.int (Short_To_Network (C.unsigned_short (Port))), SP,
984 Res'Access, Buf'Address, Buflen) /= 0
986 raise Service_Error with "Service not found";
989 -- Translate from the C format to the API format
991 return To_Service_Entry (Res);
992 end Get_Service_By_Port;
994 ---------------------
995 -- Get_Socket_Name --
996 ---------------------
998 function Get_Socket_Name
999 (Socket : Socket_Type) return Sock_Addr_Type
1001 Sin : aliased Sockaddr_In;
1002 Len : aliased C.int := Sin'Size / 8;
1004 Addr : Sock_Addr_Type := No_Sock_Addr;
1007 Res := C_Getsockname (C.int (Socket), Sin'Address, Len'Access);
1009 if Res /= Failure then
1010 To_Inet_Addr (Sin.Sin_Addr, Addr.Addr);
1011 Addr.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
1015 end Get_Socket_Name;
1017 -----------------------
1018 -- Get_Socket_Option --
1019 -----------------------
1021 function Get_Socket_Option
1022 (Socket : Socket_Type;
1023 Level : Level_Type := Socket_Level;
1024 Name : Option_Name) return Option_Type
1026 use type C.unsigned_char;
1028 V8 : aliased Two_Ints;
1030 V1 : aliased C.unsigned_char;
1031 VT : aliased Timeval;
1032 Len : aliased C.int;
1033 Add : System.Address;
1035 Opt : Option_Type (Name);
1039 when Multicast_Loop |
1041 Receive_Packet_Info =>
1076 if Res = Failure then
1077 Raise_Socket_Error (Socket_Errno);
1085 Opt.Enabled := (V4 /= 0);
1088 Opt.Enabled := (V8 (V8'First) /= 0);
1089 Opt.Seconds := Natural (V8 (V8'Last));
1093 Opt.Size := Natural (V4);
1096 Opt.Error := Resolve_Error (Integer (V4));
1098 when Add_Membership |
1100 To_Inet_Addr (To_In_Addr (V8 (V8'First)), Opt.Multicast_Address);
1101 To_Inet_Addr (To_In_Addr (V8 (V8'Last)), Opt.Local_Interface);
1103 when Multicast_If =>
1104 To_Inet_Addr (To_In_Addr (V4), Opt.Outgoing_If);
1106 when Multicast_TTL =>
1107 Opt.Time_To_Live := Integer (V1);
1109 when Multicast_Loop |
1110 Receive_Packet_Info =>
1111 Opt.Enabled := (V1 /= 0);
1115 Opt.Timeout := To_Duration (VT);
1119 end Get_Socket_Option;
1125 function Host_Name return String is
1126 Name : aliased C.char_array (1 .. 64);
1130 Res := C_Gethostname (Name'Address, Name'Length);
1132 if Res = Failure then
1133 Raise_Socket_Error (Socket_Errno);
1136 return C.To_Ada (Name);
1144 (Val : Inet_Addr_VN_Type;
1145 Hex : Boolean := False) return String
1147 -- The largest Inet_Addr_Comp_Type image occurs with IPv4. It
1148 -- has at most a length of 3 plus one '.' character.
1150 Buffer : String (1 .. 4 * Val'Length);
1151 Length : Natural := 1;
1152 Separator : Character;
1154 procedure Img10 (V : Inet_Addr_Comp_Type);
1155 -- Append to Buffer image of V in decimal format
1157 procedure Img16 (V : Inet_Addr_Comp_Type);
1158 -- Append to Buffer image of V in hexadecimal format
1164 procedure Img10 (V : Inet_Addr_Comp_Type) is
1165 Img : constant String := V'Img;
1166 Len : constant Natural := Img'Length - 1;
1168 Buffer (Length .. Length + Len - 1) := Img (2 .. Img'Last);
1169 Length := Length + Len;
1176 procedure Img16 (V : Inet_Addr_Comp_Type) is
1178 Buffer (Length) := Hex_To_Char (Natural (V / 16) + 1);
1179 Buffer (Length + 1) := Hex_To_Char (Natural (V mod 16) + 1);
1180 Length := Length + 2;
1183 -- Start of processing for Image
1192 for J in Val'Range loop
1199 if J /= Val'Last then
1200 Buffer (Length) := Separator;
1201 Length := Length + 1;
1205 return Buffer (1 .. Length - 1);
1212 function Image (Value : Inet_Addr_Type) return String is
1214 if Value.Family = Family_Inet then
1215 return Image (Inet_Addr_VN_Type (Value.Sin_V4), Hex => False);
1217 return Image (Inet_Addr_VN_Type (Value.Sin_V6), Hex => True);
1225 function Image (Value : Sock_Addr_Type) return String is
1226 Port : constant String := Value.Port'Img;
1228 return Image (Value.Addr) & ':' & Port (2 .. Port'Last);
1235 function Image (Socket : Socket_Type) return String is
1244 function Inet_Addr (Image : String) return Inet_Addr_Type is
1245 use Interfaces.C.Strings;
1249 Result : Inet_Addr_Type;
1252 -- Special case for the all-ones broadcast address: this address has the
1253 -- same in_addr_t value as Failure, and thus cannot be properly returned
1256 if Image = "255.255.255.255" then
1257 return Broadcast_Inet_Addr;
1259 -- Special case for an empty Image as on some platforms (e.g. Windows)
1260 -- calling Inet_Addr("") will not return an error.
1262 elsif Image = "" then
1263 Raise_Socket_Error (Constants.EINVAL);
1266 Img := New_String (Image);
1267 Res := C_Inet_Addr (Img);
1270 if Res = Failure then
1271 Raise_Socket_Error (Constants.EINVAL);
1274 To_Inet_Addr (To_In_Addr (Res), Result);
1282 procedure Initialize (Process_Blocking_IO : Boolean) is
1283 Expected : constant Boolean := not Constants.Thread_Blocking_IO;
1285 if Process_Blocking_IO /= Expected then
1286 raise Socket_Error with
1287 "incorrect Process_Blocking_IO setting, expected " & Expected'Img;
1297 procedure Initialize is
1299 if not Initialized then
1300 Initialized := True;
1309 function Is_Empty (Item : Socket_Set_Type) return Boolean is
1311 return Item.Last = No_Socket;
1318 function Is_IP_Address (Name : String) return Boolean is
1320 for J in Name'Range loop
1322 and then Name (J) not in '0' .. '9'
1336 (Item : Socket_Set_Type;
1337 Socket : Socket_Type) return Boolean
1340 return Item.Last /= No_Socket
1341 and then Socket <= Item.Last
1342 and then Is_Socket_In_Set (Item.Set, C.int (Socket)) /= 0;
1349 procedure Listen_Socket
1350 (Socket : Socket_Type;
1351 Length : Natural := 15)
1353 Res : constant C.int := C_Listen (C.int (Socket), C.int (Length));
1355 if Res = Failure then
1356 Raise_Socket_Error (Socket_Errno);
1364 procedure Narrow (Item : in out Socket_Set_Type) is
1365 Last : aliased C.int := C.int (Item.Last);
1367 if Item.Set /= No_Fd_Set_Access then
1368 Last_Socket_In_Set (Item.Set, Last'Unchecked_Access);
1369 Item.Last := Socket_Type (Last);
1377 function Official_Name (E : Host_Entry_Type) return String is
1379 return To_String (E.Official);
1386 function Official_Name (S : Service_Entry_Type) return String is
1388 return To_String (S.Official);
1391 --------------------
1392 -- Wait_On_Socket --
1393 --------------------
1395 procedure Wait_On_Socket
1396 (Socket : Socket_Type;
1398 Timeout : Selector_Duration;
1399 Selector : access Selector_Type := null;
1400 Status : out Selector_Status)
1402 type Local_Selector_Access is access Selector_Type;
1403 for Local_Selector_Access'Storage_Size use Selector_Type'Size;
1405 S : Selector_Access;
1406 -- Selector to use for waiting
1408 R_Fd_Set : Socket_Set_Type;
1409 W_Fd_Set : Socket_Set_Type;
1410 -- Socket sets, empty at elaboration
1413 -- Create selector if not provided by the user
1415 if Selector = null then
1417 Local_S : constant Local_Selector_Access := new Selector_Type;
1419 S := Local_S.all'Unchecked_Access;
1420 Create_Selector (S.all);
1424 S := Selector.all'Access;
1428 Set (R_Fd_Set, Socket);
1430 Set (W_Fd_Set, Socket);
1433 Check_Selector (S.all, R_Fd_Set, W_Fd_Set, Status, Timeout);
1435 -- Cleanup actions (required in all cases to avoid memory leaks)
1443 if Selector = null then
1444 Close_Selector (S.all);
1452 function Port_Number (S : Service_Entry_Type) return Port_Type is
1461 function Protocol_Name (S : Service_Entry_Type) return String is
1463 return To_String (S.Protocol);
1466 ----------------------
1467 -- Raise_Host_Error --
1468 ----------------------
1470 procedure Raise_Host_Error (H_Error : Integer) is
1472 raise Host_Error with
1473 Err_Code_Image (H_Error)
1474 & C.Strings.Value (Host_Error_Messages.Host_Error_Message (H_Error));
1475 end Raise_Host_Error;
1477 ------------------------
1478 -- Raise_Socket_Error --
1479 ------------------------
1481 procedure Raise_Socket_Error (Error : Integer) is
1482 use type C.Strings.chars_ptr;
1484 raise Socket_Error with
1485 Err_Code_Image (Error)
1486 & C.Strings.Value (Socket_Error_Message (Error));
1487 end Raise_Socket_Error;
1494 (Stream : in out Datagram_Socket_Stream_Type;
1495 Item : out Ada.Streams.Stream_Element_Array;
1496 Last : out Ada.Streams.Stream_Element_Offset)
1498 First : Ada.Streams.Stream_Element_Offset := Item'First;
1499 Index : Ada.Streams.Stream_Element_Offset := First - 1;
1500 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
1506 Item (First .. Max),
1512 -- Exit when all or zero data received. Zero means that the socket
1515 exit when Index < First or else Index = Max;
1526 (Stream : in out Stream_Socket_Stream_Type;
1527 Item : out Ada.Streams.Stream_Element_Array;
1528 Last : out Ada.Streams.Stream_Element_Offset)
1530 pragma Warnings (Off, Stream);
1532 First : Ada.Streams.Stream_Element_Offset := Item'First;
1533 Index : Ada.Streams.Stream_Element_Offset := First - 1;
1534 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
1538 Receive_Socket (Stream.Socket, Item (First .. Max), Index);
1541 -- Exit when all or zero data received. Zero means that the socket
1544 exit when Index < First or else Index = Max;
1550 --------------------
1551 -- Receive_Socket --
1552 --------------------
1554 procedure Receive_Socket
1555 (Socket : Socket_Type;
1556 Item : out Ada.Streams.Stream_Element_Array;
1557 Last : out Ada.Streams.Stream_Element_Offset;
1558 Flags : Request_Flag_Type := No_Request_Flag)
1564 C_Recv (C.int (Socket), Item'Address, Item'Length, To_Int (Flags));
1566 if Res = Failure then
1567 Raise_Socket_Error (Socket_Errno);
1570 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1573 --------------------
1574 -- Receive_Socket --
1575 --------------------
1577 procedure Receive_Socket
1578 (Socket : Socket_Type;
1579 Item : out Ada.Streams.Stream_Element_Array;
1580 Last : out Ada.Streams.Stream_Element_Offset;
1581 From : out Sock_Addr_Type;
1582 Flags : Request_Flag_Type := No_Request_Flag)
1585 Sin : aliased Sockaddr_In;
1586 Len : aliased C.int := Sin'Size / 8;
1595 Sin'Unchecked_Access,
1598 if Res = Failure then
1599 Raise_Socket_Error (Socket_Errno);
1602 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1604 To_Inet_Addr (Sin.Sin_Addr, From.Addr);
1605 From.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
1612 function Resolve_Error
1613 (Error_Value : Integer;
1614 From_Errno : Boolean := True) return Error_Type
1616 use GNAT.Sockets.Constants;
1619 if not From_Errno then
1621 when Constants.HOST_NOT_FOUND => return Unknown_Host;
1622 when Constants.TRY_AGAIN => return Host_Name_Lookup_Failure;
1623 when Constants.NO_RECOVERY => return Non_Recoverable_Error;
1624 when Constants.NO_DATA => return Unknown_Server_Error;
1625 when others => return Cannot_Resolve_Error;
1630 when ENOERROR => return Success;
1631 when EACCES => return Permission_Denied;
1632 when EADDRINUSE => return Address_Already_In_Use;
1633 when EADDRNOTAVAIL => return Cannot_Assign_Requested_Address;
1634 when EAFNOSUPPORT => return
1635 Address_Family_Not_Supported_By_Protocol;
1636 when EALREADY => return Operation_Already_In_Progress;
1637 when EBADF => return Bad_File_Descriptor;
1638 when ECONNABORTED => return Software_Caused_Connection_Abort;
1639 when ECONNREFUSED => return Connection_Refused;
1640 when ECONNRESET => return Connection_Reset_By_Peer;
1641 when EDESTADDRREQ => return Destination_Address_Required;
1642 when EFAULT => return Bad_Address;
1643 when EHOSTDOWN => return Host_Is_Down;
1644 when EHOSTUNREACH => return No_Route_To_Host;
1645 when EINPROGRESS => return Operation_Now_In_Progress;
1646 when EINTR => return Interrupted_System_Call;
1647 when EINVAL => return Invalid_Argument;
1648 when EIO => return Input_Output_Error;
1649 when EISCONN => return Transport_Endpoint_Already_Connected;
1650 when ELOOP => return Too_Many_Symbolic_Links;
1651 when EMFILE => return Too_Many_Open_Files;
1652 when EMSGSIZE => return Message_Too_Long;
1653 when ENAMETOOLONG => return File_Name_Too_Long;
1654 when ENETDOWN => return Network_Is_Down;
1655 when ENETRESET => return
1656 Network_Dropped_Connection_Because_Of_Reset;
1657 when ENETUNREACH => return Network_Is_Unreachable;
1658 when ENOBUFS => return No_Buffer_Space_Available;
1659 when ENOPROTOOPT => return Protocol_Not_Available;
1660 when ENOTCONN => return Transport_Endpoint_Not_Connected;
1661 when ENOTSOCK => return Socket_Operation_On_Non_Socket;
1662 when EOPNOTSUPP => return Operation_Not_Supported;
1663 when EPFNOSUPPORT => return Protocol_Family_Not_Supported;
1664 when EPROTONOSUPPORT => return Protocol_Not_Supported;
1665 when EPROTOTYPE => return Protocol_Wrong_Type_For_Socket;
1666 when ESHUTDOWN => return
1667 Cannot_Send_After_Transport_Endpoint_Shutdown;
1668 when ESOCKTNOSUPPORT => return Socket_Type_Not_Supported;
1669 when ETIMEDOUT => return Connection_Timed_Out;
1670 when ETOOMANYREFS => return Too_Many_References;
1671 when EWOULDBLOCK => return Resource_Temporarily_Unavailable;
1672 when others => null;
1675 return Cannot_Resolve_Error;
1678 -----------------------
1679 -- Resolve_Exception --
1680 -----------------------
1682 function Resolve_Exception
1683 (Occurrence : Exception_Occurrence) return Error_Type
1685 Id : constant Exception_Id := Exception_Identity (Occurrence);
1686 Msg : constant String := Exception_Message (Occurrence);
1693 while First <= Msg'Last
1694 and then Msg (First) not in '0' .. '9'
1699 if First > Msg'Last then
1700 return Cannot_Resolve_Error;
1704 while Last < Msg'Last
1705 and then Msg (Last + 1) in '0' .. '9'
1710 Val := Integer'Value (Msg (First .. Last));
1712 if Id = Socket_Error_Id then
1713 return Resolve_Error (Val);
1714 elsif Id = Host_Error_Id then
1715 return Resolve_Error (Val, False);
1717 return Cannot_Resolve_Error;
1719 end Resolve_Exception;
1721 --------------------
1722 -- Receive_Vector --
1723 --------------------
1725 procedure Receive_Vector
1726 (Socket : Socket_Type;
1727 Vector : Vector_Type;
1728 Count : out Ada.Streams.Stream_Element_Count)
1739 if Res = Failure then
1740 Raise_Socket_Error (Socket_Errno);
1743 Count := Ada.Streams.Stream_Element_Count (Res);
1750 procedure Send_Socket
1751 (Socket : Socket_Type;
1752 Item : Ada.Streams.Stream_Element_Array;
1753 Last : out Ada.Streams.Stream_Element_Offset;
1754 Flags : Request_Flag_Type := No_Request_Flag)
1764 Set_Forced_Flags (To_Int (Flags)));
1766 if Res = Failure then
1767 Raise_Socket_Error (Socket_Errno);
1770 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1777 procedure Send_Socket
1778 (Socket : Socket_Type;
1779 Item : Ada.Streams.Stream_Element_Array;
1780 Last : out Ada.Streams.Stream_Element_Offset;
1781 To : Sock_Addr_Type;
1782 Flags : Request_Flag_Type := No_Request_Flag)
1785 Sin : aliased Sockaddr_In;
1786 Len : constant C.int := Sin'Size / 8;
1789 Set_Family (Sin.Sin_Family, To.Family);
1790 Set_Address (Sin'Unchecked_Access, To_In_Addr (To.Addr));
1792 (Sin'Unchecked_Access,
1793 Short_To_Network (C.unsigned_short (To.Port)));
1799 Set_Forced_Flags (To_Int (Flags)),
1800 Sin'Unchecked_Access,
1803 if Res = Failure then
1804 Raise_Socket_Error (Socket_Errno);
1807 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1814 procedure Send_Vector
1815 (Socket : Socket_Type;
1816 Vector : Vector_Type;
1817 Count : out Ada.Streams.Stream_Element_Count)
1821 This_Iov_Count : C.int;
1826 while Iov_Count < Vector'Length loop
1828 pragma Warnings (Off);
1829 -- Following test may be compile time known on some targets
1831 if Vector'Length - Iov_Count > Constants.IOV_MAX then
1832 This_Iov_Count := Constants.IOV_MAX;
1834 This_Iov_Count := Vector'Length - Iov_Count;
1837 pragma Warnings (On);
1842 Vector (Vector'First + Integer (Iov_Count))'Address,
1845 if Res = Failure then
1846 Raise_Socket_Error (Socket_Errno);
1849 Count := Count + Ada.Streams.Stream_Element_Count (Res);
1850 Iov_Count := Iov_Count + This_Iov_Count;
1858 procedure Set (Item : in out Socket_Set_Type; Socket : Socket_Type) is
1860 if Item.Set = No_Fd_Set_Access then
1861 Item.Set := New_Socket_Set (No_Fd_Set_Access);
1862 Item.Last := Socket;
1864 elsif Item.Last < Socket then
1865 Item.Last := Socket;
1868 Insert_Socket_In_Set (Item.Set, C.int (Socket));
1871 ----------------------
1872 -- Set_Forced_Flags --
1873 ----------------------
1875 function Set_Forced_Flags (F : C.int) return C.int is
1876 use type C.unsigned;
1877 function To_unsigned is
1878 new Ada.Unchecked_Conversion (C.int, C.unsigned);
1880 new Ada.Unchecked_Conversion (C.unsigned, C.int);
1882 return To_int (To_unsigned (F) or Constants.MSG_Forced_Flags);
1883 end Set_Forced_Flags;
1885 -----------------------
1886 -- Set_Socket_Option --
1887 -----------------------
1889 procedure Set_Socket_Option
1890 (Socket : Socket_Type;
1891 Level : Level_Type := Socket_Level;
1892 Option : Option_Type)
1894 V8 : aliased Two_Ints;
1896 V1 : aliased C.unsigned_char;
1897 VT : aliased Timeval;
1899 Add : System.Address := Null_Address;
1908 V4 := C.int (Boolean'Pos (Option.Enabled));
1913 V8 (V8'First) := C.int (Boolean'Pos (Option.Enabled));
1914 V8 (V8'Last) := C.int (Option.Seconds);
1920 V4 := C.int (Option.Size);
1925 V4 := C.int (Boolean'Pos (True));
1929 when Add_Membership |
1931 V8 (V8'First) := To_Int (To_In_Addr (Option.Multicast_Address));
1932 V8 (V8'Last) := To_Int (To_In_Addr (Option.Local_Interface));
1936 when Multicast_If =>
1937 V4 := To_Int (To_In_Addr (Option.Outgoing_If));
1941 when Multicast_TTL =>
1942 V1 := C.unsigned_char (Option.Time_To_Live);
1946 when Multicast_Loop |
1947 Receive_Packet_Info =>
1948 V1 := C.unsigned_char (Boolean'Pos (Option.Enabled));
1954 VT := To_Timeval (Option.Timeout);
1963 Options (Option.Name),
1966 if Res = Failure then
1967 Raise_Socket_Error (Socket_Errno);
1969 end Set_Socket_Option;
1971 ----------------------
1972 -- Short_To_Network --
1973 ----------------------
1975 function Short_To_Network (S : C.unsigned_short) return C.unsigned_short is
1976 use type C.unsigned_short;
1979 -- Big-endian case. No conversion needed. On these platforms,
1980 -- htons() defaults to a null procedure.
1982 pragma Warnings (Off);
1983 -- Since the test can generate "always True/False" warning
1985 if Default_Bit_Order = High_Order_First then
1988 pragma Warnings (On);
1990 -- Little-endian case. We must swap the high and low bytes of this
1991 -- short to make the port number network compliant.
1994 return (S / 256) + (S mod 256) * 256;
1996 end Short_To_Network;
1998 ---------------------
1999 -- Shutdown_Socket --
2000 ---------------------
2002 procedure Shutdown_Socket
2003 (Socket : Socket_Type;
2004 How : Shutmode_Type := Shut_Read_Write)
2009 Res := C_Shutdown (C.int (Socket), Shutmodes (How));
2011 if Res = Failure then
2012 Raise_Socket_Error (Socket_Errno);
2014 end Shutdown_Socket;
2021 (Socket : Socket_Type;
2022 Send_To : Sock_Addr_Type) return Stream_Access
2024 S : Datagram_Socket_Stream_Access;
2027 S := new Datagram_Socket_Stream_Type;
2030 S.From := Get_Socket_Name (Socket);
2031 return Stream_Access (S);
2038 function Stream (Socket : Socket_Type) return Stream_Access is
2039 S : Stream_Socket_Stream_Access;
2041 S := new Stream_Socket_Stream_Type;
2043 return Stream_Access (S);
2050 function To_C (Socket : Socket_Type) return Integer is
2052 return Integer (Socket);
2059 function To_Duration (Val : Timeval) return Timeval_Duration is
2061 return Natural (Val.Tv_Sec) * 1.0 + Natural (Val.Tv_Usec) * 1.0E-6;
2068 function To_Host_Entry (E : Hostent) return Host_Entry_Type is
2071 Official : constant String :=
2072 C.Strings.Value (E.H_Name);
2074 Aliases : constant Chars_Ptr_Array :=
2075 Chars_Ptr_Pointers.Value (E.H_Aliases);
2076 -- H_Aliases points to a list of name aliases. The list is terminated by
2079 Addresses : constant In_Addr_Access_Array :=
2080 In_Addr_Access_Pointers.Value (E.H_Addr_List);
2081 -- H_Addr_List points to a list of binary addresses (in network byte
2082 -- order). The list is terminated by a NULL pointer.
2084 -- H_Length is not used because it is currently only set to 4.
2085 -- H_Addrtype is always AF_INET
2087 Result : Host_Entry_Type
2088 (Aliases_Length => Aliases'Length - 1,
2089 Addresses_Length => Addresses'Length - 1);
2090 -- The last element is a null pointer
2096 Result.Official := To_Name (Official);
2098 Source := Aliases'First;
2099 Target := Result.Aliases'First;
2100 while Target <= Result.Aliases_Length loop
2101 Result.Aliases (Target) :=
2102 To_Name (C.Strings.Value (Aliases (Source)));
2103 Source := Source + 1;
2104 Target := Target + 1;
2107 Source := Addresses'First;
2108 Target := Result.Addresses'First;
2109 while Target <= Result.Addresses_Length loop
2110 To_Inet_Addr (Addresses (Source).all, Result.Addresses (Target));
2111 Source := Source + 1;
2112 Target := Target + 1;
2122 function To_In_Addr (Addr : Inet_Addr_Type) return In_Addr is
2124 if Addr.Family = Family_Inet then
2125 return (S_B1 => C.unsigned_char (Addr.Sin_V4 (1)),
2126 S_B2 => C.unsigned_char (Addr.Sin_V4 (2)),
2127 S_B3 => C.unsigned_char (Addr.Sin_V4 (3)),
2128 S_B4 => C.unsigned_char (Addr.Sin_V4 (4)));
2131 raise Socket_Error with "IPv6 not supported";
2138 procedure To_Inet_Addr
2140 Result : out Inet_Addr_Type) is
2142 Result.Sin_V4 (1) := Inet_Addr_Comp_Type (Addr.S_B1);
2143 Result.Sin_V4 (2) := Inet_Addr_Comp_Type (Addr.S_B2);
2144 Result.Sin_V4 (3) := Inet_Addr_Comp_Type (Addr.S_B3);
2145 Result.Sin_V4 (4) := Inet_Addr_Comp_Type (Addr.S_B4);
2152 function To_Int (F : Request_Flag_Type) return C.int
2154 Current : Request_Flag_Type := F;
2155 Result : C.int := 0;
2158 for J in Flags'Range loop
2159 exit when Current = 0;
2161 if Current mod 2 /= 0 then
2162 if Flags (J) = -1 then
2163 Raise_Socket_Error (Constants.EOPNOTSUPP);
2166 Result := Result + Flags (J);
2169 Current := Current / 2;
2179 function To_Name (N : String) return Name_Type is
2181 return Name_Type'(N'Length, N);
2184 ----------------------
2185 -- To_Service_Entry --
2186 ----------------------
2188 function To_Service_Entry (E : Servent) return Service_Entry_Type is
2191 Official : constant String := C.Strings.Value (E.S_Name);
2193 Aliases : constant Chars_Ptr_Array :=
2194 Chars_Ptr_Pointers.Value (E.S_Aliases);
2195 -- S_Aliases points to a list of name aliases. The list is
2196 -- terminated by a NULL pointer.
2198 Protocol : constant String := C.Strings.Value (E.S_Proto);
2200 Result : Service_Entry_Type (Aliases_Length => Aliases'Length - 1);
2201 -- The last element is a null pointer
2207 Result.Official := To_Name (Official);
2209 Source := Aliases'First;
2210 Target := Result.Aliases'First;
2211 while Target <= Result.Aliases_Length loop
2212 Result.Aliases (Target) :=
2213 To_Name (C.Strings.Value (Aliases (Source)));
2214 Source := Source + 1;
2215 Target := Target + 1;
2219 Port_Type (Network_To_Short (C.unsigned_short (E.S_Port)));
2221 Result.Protocol := To_Name (Protocol);
2223 end To_Service_Entry;
2229 function To_String (HN : Name_Type) return String is
2231 return HN.Name (1 .. HN.Length);
2238 function To_Timeval (Val : Timeval_Duration) return Timeval is
2243 -- If zero, set result as zero (otherwise it gets rounded down to -1)
2249 -- Normal case where we do round down
2252 S := time_t (Val - 0.5);
2253 uS := suseconds_t (1_000_000 * (Val - Selector_Duration (S)));
2264 (Stream : in out Datagram_Socket_Stream_Type;
2265 Item : Ada.Streams.Stream_Element_Array)
2267 pragma Warnings (Off, Stream);
2269 First : Ada.Streams.Stream_Element_Offset := Item'First;
2270 Index : Ada.Streams.Stream_Element_Offset := First - 1;
2271 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
2277 Item (First .. Max),
2281 -- Exit when all or zero data sent. Zero means that the socket has
2282 -- been closed by peer.
2284 exit when Index < First or else Index = Max;
2289 if Index /= Max then
2299 (Stream : in out Stream_Socket_Stream_Type;
2300 Item : Ada.Streams.Stream_Element_Array)
2302 pragma Warnings (Off, Stream);
2304 First : Ada.Streams.Stream_Element_Offset := Item'First;
2305 Index : Ada.Streams.Stream_Element_Offset := First - 1;
2306 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
2310 Send_Socket (Stream.Socket, Item (First .. Max), Index);
2312 -- Exit when all or zero data sent. Zero means that the socket has
2313 -- been closed by peer.
2315 exit when Index < First or else Index = Max;
2320 if Index /= Max then