1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S --
9 -- Copyright (C) 2001-2005 Ada Core Technologies, Inc. --
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.Constants;
41 with GNAT.Sockets.Thin; use GNAT.Sockets.Thin;
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 use type C.int, System.Address;
54 Finalized : Boolean := False;
55 Initialized : Boolean := False;
57 ENOERROR : constant := 0;
59 -- Correspondance tables
61 Families : constant array (Family_Type) of C.int :=
62 (Family_Inet => Constants.AF_INET,
63 Family_Inet6 => Constants.AF_INET6);
65 Levels : constant array (Level_Type) of C.int :=
66 (Socket_Level => Constants.SOL_SOCKET,
67 IP_Protocol_For_IP_Level => Constants.IPPROTO_IP,
68 IP_Protocol_For_UDP_Level => Constants.IPPROTO_UDP,
69 IP_Protocol_For_TCP_Level => Constants.IPPROTO_TCP);
71 Modes : constant array (Mode_Type) of C.int :=
72 (Socket_Stream => Constants.SOCK_STREAM,
73 Socket_Datagram => Constants.SOCK_DGRAM);
75 Shutmodes : constant array (Shutmode_Type) of C.int :=
76 (Shut_Read => Constants.SHUT_RD,
77 Shut_Write => Constants.SHUT_WR,
78 Shut_Read_Write => Constants.SHUT_RDWR);
80 Requests : constant array (Request_Name) of C.int :=
81 (Non_Blocking_IO => Constants.FIONBIO,
82 N_Bytes_To_Read => Constants.FIONREAD);
84 Options : constant array (Option_Name) of C.int :=
85 (Keep_Alive => Constants.SO_KEEPALIVE,
86 Reuse_Address => Constants.SO_REUSEADDR,
87 Broadcast => Constants.SO_BROADCAST,
88 Send_Buffer => Constants.SO_SNDBUF,
89 Receive_Buffer => Constants.SO_RCVBUF,
90 Linger => Constants.SO_LINGER,
91 Error => Constants.SO_ERROR,
92 No_Delay => Constants.TCP_NODELAY,
93 Add_Membership => Constants.IP_ADD_MEMBERSHIP,
94 Drop_Membership => Constants.IP_DROP_MEMBERSHIP,
95 Multicast_TTL => Constants.IP_MULTICAST_TTL,
96 Multicast_Loop => Constants.IP_MULTICAST_LOOP);
98 Flags : constant array (0 .. 3) of C.int :=
99 (0 => Constants.MSG_OOB, -- Process_Out_Of_Band_Data
100 1 => Constants.MSG_PEEK, -- Peek_At_Incoming_Data
101 2 => Constants.MSG_WAITALL, -- Wait_For_A_Full_Reception
102 3 => Constants.MSG_EOR); -- Send_End_Of_Record
104 Socket_Error_Id : constant Exception_Id := Socket_Error'Identity;
105 Host_Error_Id : constant Exception_Id := Host_Error'Identity;
107 Hex_To_Char : constant String (1 .. 16) := "0123456789ABCDEF";
108 -- Use to print in hexadecimal format
110 function To_In_Addr is new Ada.Unchecked_Conversion (C.int, In_Addr);
111 function To_Int is new Ada.Unchecked_Conversion (In_Addr, C.int);
113 -----------------------
114 -- Local subprograms --
115 -----------------------
117 function Resolve_Error
118 (Error_Value : Integer;
119 From_Errno : Boolean := True) return Error_Type;
120 -- Associate an enumeration value (error_type) to en error value (errno).
121 -- From_Errno prevents from mixing h_errno with errno.
123 function To_Name (N : String) return Name_Type;
124 function To_String (HN : Name_Type) return String;
125 -- Conversion functions
127 function To_Int (F : Request_Flag_Type) return C.int;
128 -- Return the int value corresponding to the specified flags combination
130 function Set_Forced_Flags (F : C.int) return C.int;
131 -- Return F with the bits from Constants.MSG_Forced_Flags forced set
133 function Short_To_Network
134 (S : C.unsigned_short) return C.unsigned_short;
135 pragma Inline (Short_To_Network);
136 -- Convert a port number into a network port number
138 function Network_To_Short
139 (S : C.unsigned_short) return C.unsigned_short
140 renames Short_To_Network;
141 -- Symetric operation
144 (Val : Inet_Addr_VN_Type;
145 Hex : Boolean := False) return String;
146 -- Output an array of inet address components in hex or decimal mode
148 function Is_IP_Address (Name : String) return Boolean;
149 -- Return true when Name is an IP address in standard dot notation
151 function To_In_Addr (Addr : Inet_Addr_Type) return Thin.In_Addr;
152 function To_Inet_Addr (Addr : In_Addr) return Inet_Addr_Type;
153 -- Conversion functions
155 function To_Host_Entry (E : Hostent) return Host_Entry_Type;
156 -- Conversion function
158 function To_Service_Entry (E : Servent) return Service_Entry_Type;
159 -- Conversion function
161 function To_Timeval (Val : Selector_Duration) return Timeval;
162 -- Separate Val in seconds and microseconds
164 procedure Raise_Socket_Error (Error : Integer);
165 -- Raise Socket_Error with an exception message describing the error code
167 procedure Raise_Host_Error (Error : Integer);
168 -- Raise Host_Error exception with message describing error code (note
169 -- hstrerror seems to be obsolete).
171 procedure Narrow (Item : in out Socket_Set_Type);
172 -- Update Last as it may be greater than the real last socket
174 -- Types needed for Datagram_Socket_Stream_Type
176 type Datagram_Socket_Stream_Type is new Root_Stream_Type with record
177 Socket : Socket_Type;
179 From : Sock_Addr_Type;
182 type Datagram_Socket_Stream_Access is
183 access all Datagram_Socket_Stream_Type;
186 (Stream : in out Datagram_Socket_Stream_Type;
187 Item : out Ada.Streams.Stream_Element_Array;
188 Last : out Ada.Streams.Stream_Element_Offset);
191 (Stream : in out Datagram_Socket_Stream_Type;
192 Item : Ada.Streams.Stream_Element_Array);
194 -- Types needed for Stream_Socket_Stream_Type
196 type Stream_Socket_Stream_Type is new Root_Stream_Type with record
197 Socket : Socket_Type;
200 type Stream_Socket_Stream_Access is
201 access all Stream_Socket_Stream_Type;
204 (Stream : in out Stream_Socket_Stream_Type;
205 Item : out Ada.Streams.Stream_Element_Array;
206 Last : out Ada.Streams.Stream_Element_Offset);
209 (Stream : in out Stream_Socket_Stream_Type;
210 Item : Ada.Streams.Stream_Element_Array);
216 function "+" (L, R : Request_Flag_Type) return Request_Flag_Type is
225 procedure Abort_Selector (Selector : Selector_Type) is
226 Buf : aliased Character := ASCII.NUL;
230 -- Send an empty array to unblock C select system call
232 Res := C_Send (C.int (Selector.W_Sig_Socket), Buf'Address, 1,
233 Constants.MSG_Forced_Flags);
234 if Res = Failure then
235 Raise_Socket_Error (Socket_Errno);
243 procedure Accept_Socket
244 (Server : Socket_Type;
245 Socket : out Socket_Type;
246 Address : out Sock_Addr_Type)
249 Sin : aliased Sockaddr_In;
250 Len : aliased C.int := Sin'Size / 8;
253 Res := C_Accept (C.int (Server), Sin'Address, Len'Access);
255 if Res = Failure then
256 Raise_Socket_Error (Socket_Errno);
259 Socket := Socket_Type (Res);
261 Address.Addr := To_Inet_Addr (Sin.Sin_Addr);
262 Address.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
270 (E : Host_Entry_Type;
271 N : Positive := 1) return Inet_Addr_Type
274 return E.Addresses (N);
277 ----------------------
278 -- Addresses_Length --
279 ----------------------
281 function Addresses_Length (E : Host_Entry_Type) return Natural is
283 return E.Addresses_Length;
284 end Addresses_Length;
291 (E : Host_Entry_Type;
292 N : Positive := 1) return String
295 return To_String (E.Aliases (N));
303 (S : Service_Entry_Type;
304 N : Positive := 1) return String
307 return To_String (S.Aliases (N));
314 function Aliases_Length (E : Host_Entry_Type) return Natural is
316 return E.Aliases_Length;
323 function Aliases_Length (S : Service_Entry_Type) return Natural is
325 return S.Aliases_Length;
332 procedure Bind_Socket
333 (Socket : Socket_Type;
334 Address : Sock_Addr_Type)
337 Sin : aliased Sockaddr_In;
338 Len : constant C.int := Sin'Size / 8;
341 if Address.Family = Family_Inet6 then
345 Set_Length (Sin'Unchecked_Access, Len);
346 Set_Family (Sin'Unchecked_Access, Families (Address.Family));
348 (Sin'Unchecked_Access,
349 Short_To_Network (C.unsigned_short (Address.Port)));
351 Res := C_Bind (C.int (Socket), Sin'Address, Len);
353 if Res = Failure then
354 Raise_Socket_Error (Socket_Errno);
362 procedure Check_Selector
363 (Selector : in out Selector_Type;
364 R_Socket_Set : in out Socket_Set_Type;
365 W_Socket_Set : in out Socket_Set_Type;
366 Status : out Selector_Status;
367 Timeout : Selector_Duration := Forever)
369 E_Socket_Set : Socket_Set_Type; -- (No_Socket, No_Socket_Set)
372 (Selector, R_Socket_Set, W_Socket_Set, E_Socket_Set, Status, Timeout);
375 procedure Check_Selector
376 (Selector : in out Selector_Type;
377 R_Socket_Set : in out Socket_Set_Type;
378 W_Socket_Set : in out Socket_Set_Type;
379 E_Socket_Set : in out Socket_Set_Type;
380 Status : out Selector_Status;
381 Timeout : Selector_Duration := Forever)
385 RSet : Socket_Set_Type;
386 WSet : Socket_Set_Type;
387 ESet : Socket_Set_Type;
388 TVal : aliased Timeval;
389 TPtr : Timeval_Access;
394 -- No timeout or Forever is indicated by a null timeval pointer
396 if Timeout = Forever then
399 TVal := To_Timeval (Timeout);
400 TPtr := TVal'Unchecked_Access;
403 -- Copy R_Socket_Set in RSet and add read signalling socket
405 RSet := (Set => New_Socket_Set (R_Socket_Set.Set),
406 Last => R_Socket_Set.Last);
407 Set (RSet, Selector.R_Sig_Socket);
409 -- Copy W_Socket_Set in WSet
411 WSet := (Set => New_Socket_Set (W_Socket_Set.Set),
412 Last => W_Socket_Set.Last);
414 -- Copy E_Socket_Set in ESet
416 ESet := (Set => New_Socket_Set (E_Socket_Set.Set),
417 Last => E_Socket_Set.Last);
419 Last := C.int'Max (C.int'Max (C.int (RSet.Last),
431 if Res = Failure then
432 Raise_Socket_Error (Socket_Errno);
435 -- If Select was resumed because of read signalling socket, read this
436 -- data and remove socket from set.
438 if Is_Set (RSet, Selector.R_Sig_Socket) then
439 Clear (RSet, Selector.R_Sig_Socket);
445 Res := C_Recv (C.int (Selector.R_Sig_Socket), Buf'Address, 1, 0);
447 if Res = Failure then
448 Raise_Socket_Error (Socket_Errno);
458 -- Update RSet, WSet and ESet in regard to their new socket sets
464 -- Reset RSet as it should be if R_Sig_Socket was not added
466 if Is_Empty (RSet) then
470 if Is_Empty (WSet) then
474 if Is_Empty (ESet) then
478 -- Deliver RSet, WSet and ESet
480 Empty (R_Socket_Set);
481 R_Socket_Set := RSet;
483 Empty (W_Socket_Set);
484 W_Socket_Set := WSet;
486 Empty (E_Socket_Set);
487 E_Socket_Set := ESet;
495 (Item : in out Socket_Set_Type;
496 Socket : Socket_Type)
498 Last : aliased C.int := C.int (Item.Last);
500 if Item.Last /= No_Socket then
501 Remove_Socket_From_Set (Item.Set, C.int (Socket));
502 Last_Socket_In_Set (Item.Set, Last'Unchecked_Access);
503 Item.Last := Socket_Type (Last);
511 -- Comments needed below ???
512 -- Why are exceptions ignored ???
514 procedure Close_Selector (Selector : in out Selector_Type) is
517 Close_Socket (Selector.R_Sig_Socket);
524 Close_Socket (Selector.W_Sig_Socket);
535 procedure Close_Socket (Socket : Socket_Type) is
539 Res := C_Close (C.int (Socket));
541 if Res = Failure then
542 Raise_Socket_Error (Socket_Errno);
550 procedure Connect_Socket
551 (Socket : Socket_Type;
552 Server : in out Sock_Addr_Type)
555 Sin : aliased Sockaddr_In;
556 Len : constant C.int := Sin'Size / 8;
559 if Server.Family = Family_Inet6 then
563 Set_Length (Sin'Unchecked_Access, Len);
564 Set_Family (Sin'Unchecked_Access, Families (Server.Family));
565 Set_Address (Sin'Unchecked_Access, To_In_Addr (Server.Addr));
567 (Sin'Unchecked_Access,
568 Short_To_Network (C.unsigned_short (Server.Port)));
570 Res := C_Connect (C.int (Socket), Sin'Address, Len);
572 if Res = Failure then
573 Raise_Socket_Error (Socket_Errno);
581 procedure Control_Socket
582 (Socket : Socket_Type;
583 Request : in out Request_Type)
590 when Non_Blocking_IO =>
591 Arg := C.int (Boolean'Pos (Request.Enabled));
593 when N_Bytes_To_Read =>
600 Requests (Request.Name),
601 Arg'Unchecked_Access);
603 if Res = Failure then
604 Raise_Socket_Error (Socket_Errno);
608 when Non_Blocking_IO =>
611 when N_Bytes_To_Read =>
612 Request.Size := Natural (Arg);
621 (Source : Socket_Set_Type;
622 Target : in out Socket_Set_Type)
626 if Source.Last /= No_Socket then
627 Target.Set := New_Socket_Set (Source.Set);
628 Target.Last := Source.Last;
632 ---------------------
633 -- Create_Selector --
634 ---------------------
636 procedure Create_Selector (Selector : out Selector_Type) is
641 Sin : aliased Sockaddr_In;
642 Len : aliased C.int := Sin'Size / 8;
646 -- We open two signalling sockets. One of them is used to send data to
647 -- the other, which is included in a C_Select socket set. The
648 -- communication is used to force the call to C_Select to complete, and
649 -- the waiting task to resume its execution.
651 -- Create a listening socket
653 S0 := C_Socket (Constants.AF_INET, Constants.SOCK_STREAM, 0);
656 Raise_Socket_Error (Socket_Errno);
659 -- Bind the socket to any unused port on localhost
661 Sin.Sin_Addr.S_B1 := 127;
662 Sin.Sin_Addr.S_B2 := 0;
663 Sin.Sin_Addr.S_B3 := 0;
664 Sin.Sin_Addr.S_B4 := 1;
667 Res := C_Bind (S0, Sin'Address, Len);
669 if Res = Failure then
672 Raise_Socket_Error (Err);
675 -- Get the port used by the socket
677 Res := C_Getsockname (S0, Sin'Address, Len'Access);
679 if Res = Failure then
682 Raise_Socket_Error (Err);
685 -- Set backlog to 1 to guarantee that exactly one call to connect(2)
688 Res := C_Listen (S0, 1);
690 if Res = Failure then
693 Raise_Socket_Error (Err);
696 S1 := C_Socket (Constants.AF_INET, Constants.SOCK_STREAM, 0);
701 Raise_Socket_Error (Err);
704 -- Do a connect and accept the connection
706 Res := C_Connect (S1, Sin'Address, Len);
708 if Res = Failure then
712 Raise_Socket_Error (Err);
715 -- Since the call to connect(2) has suceeded and the backlog limit on
716 -- the listening socket is 1, we know that there is now exactly one
717 -- pending connection on S0, which is the one from S1.
719 S2 := C_Accept (S0, Sin'Address, Len'Access);
725 Raise_Socket_Error (Err);
730 if Res = Failure then
731 Raise_Socket_Error (Socket_Errno);
734 Selector.R_Sig_Socket := Socket_Type (S1);
735 Selector.W_Sig_Socket := Socket_Type (S2);
742 procedure Create_Socket
743 (Socket : out Socket_Type;
744 Family : Family_Type := Family_Inet;
745 Mode : Mode_Type := Socket_Stream)
750 Res := C_Socket (Families (Family), Modes (Mode), 0);
752 if Res = Failure then
753 Raise_Socket_Error (Socket_Errno);
756 Socket := Socket_Type (Res);
763 procedure Empty (Item : in out Socket_Set_Type) is
765 if Item.Set /= No_Socket_Set then
766 Free_Socket_Set (Item.Set);
767 Item.Set := No_Socket_Set;
770 Item.Last := No_Socket;
777 procedure Finalize is
792 (Item : in out Socket_Set_Type;
793 Socket : out Socket_Type)
796 L : aliased C.int := C.int (Item.Last);
799 if Item.Last /= No_Socket then
801 (Item.Set, L'Unchecked_Access, S'Unchecked_Access);
802 Item.Last := Socket_Type (L);
803 Socket := Socket_Type (S);
813 function Get_Address (Stream : Stream_Access) return Sock_Addr_Type is
815 if Stream = null then
817 elsif Stream.all in Datagram_Socket_Stream_Type then
818 return Datagram_Socket_Stream_Type (Stream.all).From;
820 return Get_Peer_Name (Stream_Socket_Stream_Type (Stream.all).Socket);
824 -------------------------
825 -- Get_Host_By_Address --
826 -------------------------
828 function Get_Host_By_Address
829 (Address : Inet_Addr_Type;
830 Family : Family_Type := Family_Inet) return Host_Entry_Type
832 pragma Unreferenced (Family);
834 HA : aliased In_Addr := To_In_Addr (Address);
835 Res : Hostent_Access;
839 -- This C function is not always thread-safe. Protect against
840 -- concurrent access.
843 Res := C_Gethostbyaddr (HA'Address, HA'Size / 8, Constants.AF_INET);
848 Raise_Host_Error (Err);
851 -- Translate from the C format to the API format
854 HE : constant Host_Entry_Type := To_Host_Entry (Res.all);
860 end Get_Host_By_Address;
862 ----------------------
863 -- Get_Host_By_Name --
864 ----------------------
866 function Get_Host_By_Name (Name : String) return Host_Entry_Type is
867 HN : constant C.char_array := C.To_C (Name);
868 Res : Hostent_Access;
872 -- Detect IP address name and redirect to Inet_Addr
874 if Is_IP_Address (Name) then
875 return Get_Host_By_Address (Inet_Addr (Name));
878 -- This C function is not always thread-safe. Protect against
879 -- concurrent access.
882 Res := C_Gethostbyname (HN);
887 Raise_Host_Error (Err);
890 -- Translate from the C format to the API format
893 HE : constant Host_Entry_Type := To_Host_Entry (Res.all);
898 end Get_Host_By_Name;
904 function Get_Peer_Name (Socket : Socket_Type) return Sock_Addr_Type is
905 Sin : aliased Sockaddr_In;
906 Len : aliased C.int := Sin'Size / 8;
907 Res : Sock_Addr_Type (Family_Inet);
910 if C_Getpeername (C.int (Socket), Sin'Address, Len'Access) = Failure then
911 Raise_Socket_Error (Socket_Errno);
914 Res.Addr := To_Inet_Addr (Sin.Sin_Addr);
915 Res.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
920 -------------------------
921 -- Get_Service_By_Name --
922 -------------------------
924 function Get_Service_By_Name
926 Protocol : String) return Service_Entry_Type
928 SN : constant C.char_array := C.To_C (Name);
929 SP : constant C.char_array := C.To_C (Protocol);
930 Res : Servent_Access;
933 -- This C function is not always thread-safe. Protect against
934 -- concurrent access.
937 Res := C_Getservbyname (SN, SP);
941 Ada.Exceptions.Raise_Exception
942 (Service_Error'Identity, "Service not found");
945 -- Translate from the C format to the API format
948 SE : constant Service_Entry_Type := To_Service_Entry (Res.all);
954 end Get_Service_By_Name;
956 -------------------------
957 -- Get_Service_By_Port --
958 -------------------------
960 function Get_Service_By_Port
962 Protocol : String) return Service_Entry_Type
964 SP : constant C.char_array := C.To_C (Protocol);
965 Res : Servent_Access;
968 -- This C function is not always thread-safe. Protect against
969 -- concurrent access.
972 Res := C_Getservbyport
973 (C.int (Short_To_Network (C.unsigned_short (Port))), SP);
977 Ada.Exceptions.Raise_Exception
978 (Service_Error'Identity, "Service not found");
981 -- Translate from the C format to the API format
984 SE : constant Service_Entry_Type := To_Service_Entry (Res.all);
990 end Get_Service_By_Port;
992 ---------------------
993 -- Get_Socket_Name --
994 ---------------------
996 function Get_Socket_Name
997 (Socket : Socket_Type) return Sock_Addr_Type
999 Sin : aliased Sockaddr_In;
1000 Len : aliased C.int := Sin'Size / 8;
1002 Addr : Sock_Addr_Type := No_Sock_Addr;
1005 Res := C_Getsockname (C.int (Socket), Sin'Address, Len'Access);
1006 if Res /= Failure then
1007 Addr.Addr := To_Inet_Addr (Sin.Sin_Addr);
1008 Addr.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
1012 end Get_Socket_Name;
1014 -----------------------
1015 -- Get_Socket_Option --
1016 -----------------------
1018 function Get_Socket_Option
1019 (Socket : Socket_Type;
1020 Level : Level_Type := Socket_Level;
1021 Name : Option_Name) return Option_Type
1023 use type C.unsigned_char;
1025 V8 : aliased Two_Int;
1027 V1 : aliased C.unsigned_char;
1028 Len : aliased C.int;
1029 Add : System.Address;
1031 Opt : Option_Type (Name);
1035 when Multicast_Loop |
1063 Add, Len'Unchecked_Access);
1065 if Res = Failure then
1066 Raise_Socket_Error (Socket_Errno);
1074 Opt.Enabled := (V4 /= 0);
1077 Opt.Enabled := (V8 (V8'First) /= 0);
1078 Opt.Seconds := Natural (V8 (V8'Last));
1082 Opt.Size := Natural (V4);
1085 Opt.Error := Resolve_Error (Integer (V4));
1087 when Add_Membership |
1089 Opt.Multicast_Address := To_Inet_Addr (To_In_Addr (V8 (V8'First)));
1090 Opt.Local_Interface := To_Inet_Addr (To_In_Addr (V8 (V8'Last)));
1092 when Multicast_TTL =>
1093 Opt.Time_To_Live := Integer (V1);
1095 when Multicast_Loop =>
1096 Opt.Enabled := (V1 /= 0);
1101 end Get_Socket_Option;
1107 function Host_Name return String is
1108 Name : aliased C.char_array (1 .. 64);
1112 Res := C_Gethostname (Name'Address, Name'Length);
1114 if Res = Failure then
1115 Raise_Socket_Error (Socket_Errno);
1118 return C.To_Ada (Name);
1126 (Val : Inet_Addr_VN_Type;
1127 Hex : Boolean := False) return String
1129 -- The largest Inet_Addr_Comp_Type image occurs with IPv4. It
1130 -- has at most a length of 3 plus one '.' character.
1132 Buffer : String (1 .. 4 * Val'Length);
1133 Length : Natural := 1;
1134 Separator : Character;
1136 procedure Img10 (V : Inet_Addr_Comp_Type);
1137 -- Append to Buffer image of V in decimal format
1139 procedure Img16 (V : Inet_Addr_Comp_Type);
1140 -- Append to Buffer image of V in hexadecimal format
1146 procedure Img10 (V : Inet_Addr_Comp_Type) is
1147 Img : constant String := V'Img;
1148 Len : constant Natural := Img'Length - 1;
1150 Buffer (Length .. Length + Len - 1) := Img (2 .. Img'Last);
1151 Length := Length + Len;
1158 procedure Img16 (V : Inet_Addr_Comp_Type) is
1160 Buffer (Length) := Hex_To_Char (Natural (V / 16) + 1);
1161 Buffer (Length + 1) := Hex_To_Char (Natural (V mod 16) + 1);
1162 Length := Length + 2;
1165 -- Start of processing for Image
1174 for J in Val'Range loop
1181 if J /= Val'Last then
1182 Buffer (Length) := Separator;
1183 Length := Length + 1;
1187 return Buffer (1 .. Length - 1);
1194 function Image (Value : Inet_Addr_Type) return String is
1196 if Value.Family = Family_Inet then
1197 return Image (Inet_Addr_VN_Type (Value.Sin_V4), Hex => False);
1199 return Image (Inet_Addr_VN_Type (Value.Sin_V6), Hex => True);
1207 function Image (Value : Sock_Addr_Type) return String is
1208 Port : constant String := Value.Port'Img;
1210 return Image (Value.Addr) & ':' & Port (2 .. Port'Last);
1217 function Image (Socket : Socket_Type) return String is
1226 function Inet_Addr (Image : String) return Inet_Addr_Type is
1227 use Interfaces.C.Strings;
1233 -- Special case for the all-ones broadcast address: this address
1234 -- has the same in_addr_t value as Failure, and thus cannot be
1235 -- properly returned by inet_addr(3).
1237 if Image = "255.255.255.255" then
1238 return Broadcast_Inet_Addr;
1240 -- Special case for an empty Image as on some platforms (e.g. Windows)
1241 -- calling Inet_Addr("") will not return an error.
1243 elsif Image = "" then
1244 Raise_Socket_Error (Constants.EINVAL);
1247 Img := New_String (Image);
1248 Res := C_Inet_Addr (Img);
1251 if Res = Failure then
1252 Raise_Socket_Error (Constants.EINVAL);
1255 return To_Inet_Addr (To_In_Addr (Res));
1262 procedure Initialize (Process_Blocking_IO : Boolean := False) is
1264 if not Initialized then
1265 Initialized := True;
1266 Thin.Initialize (Process_Blocking_IO);
1274 function Is_Empty (Item : Socket_Set_Type) return Boolean is
1276 return Item.Last = No_Socket;
1283 function Is_IP_Address (Name : String) return Boolean is
1285 for J in Name'Range loop
1287 and then Name (J) not in '0' .. '9'
1301 (Item : Socket_Set_Type;
1302 Socket : Socket_Type) return Boolean
1305 return Item.Last /= No_Socket
1306 and then Socket <= Item.Last
1307 and then Is_Socket_In_Set (Item.Set, C.int (Socket)) /= 0;
1314 procedure Listen_Socket
1315 (Socket : Socket_Type;
1316 Length : Positive := 15)
1318 Res : constant C.int := C_Listen (C.int (Socket), C.int (Length));
1320 if Res = Failure then
1321 Raise_Socket_Error (Socket_Errno);
1329 procedure Narrow (Item : in out Socket_Set_Type) is
1330 Last : aliased C.int := C.int (Item.Last);
1332 if Item.Set /= No_Socket_Set then
1333 Last_Socket_In_Set (Item.Set, Last'Unchecked_Access);
1334 Item.Last := Socket_Type (Last);
1342 function Official_Name (E : Host_Entry_Type) return String is
1344 return To_String (E.Official);
1351 function Official_Name (S : Service_Entry_Type) return String is
1353 return To_String (S.Official);
1360 function Port_Number (S : Service_Entry_Type) return Port_Type is
1369 function Protocol_Name (S : Service_Entry_Type) return String is
1371 return To_String (S.Protocol);
1374 ----------------------
1375 -- Raise_Host_Error --
1376 ----------------------
1378 procedure Raise_Host_Error (Error : Integer) is
1380 function Host_Error_Message return String;
1381 -- We do not use a C function like strerror because hstrerror
1382 -- that would correspond seems to be obsolete. Return
1383 -- appropriate string for error value.
1385 ------------------------
1386 -- Host_Error_Message --
1387 ------------------------
1389 function Host_Error_Message return String is
1392 when Constants.HOST_NOT_FOUND => return "Host not found";
1393 when Constants.TRY_AGAIN => return "Try again";
1394 when Constants.NO_RECOVERY => return "No recovery";
1395 when Constants.NO_DATA => return "No address";
1396 when others => return "Unknown error";
1398 end Host_Error_Message;
1400 -- Start of processing for Raise_Host_Error
1403 Ada.Exceptions.Raise_Exception (Host_Error'Identity, Host_Error_Message);
1404 end Raise_Host_Error;
1406 ------------------------
1407 -- Raise_Socket_Error --
1408 ------------------------
1410 procedure Raise_Socket_Error (Error : Integer) is
1411 use type C.Strings.chars_ptr;
1413 function Image (E : Integer) return String;
1419 function Image (E : Integer) return String is
1420 Msg : String := E'Img & "] ";
1422 Msg (Msg'First) := '[';
1426 -- Start of processing for Raise_Socket_Error
1429 Ada.Exceptions.Raise_Exception
1430 (Socket_Error'Identity,
1431 Image (Error) & C.Strings.Value (Socket_Error_Message (Error)));
1432 end Raise_Socket_Error;
1439 (Stream : in out Datagram_Socket_Stream_Type;
1440 Item : out Ada.Streams.Stream_Element_Array;
1441 Last : out Ada.Streams.Stream_Element_Offset)
1443 First : Ada.Streams.Stream_Element_Offset := Item'First;
1444 Index : Ada.Streams.Stream_Element_Offset := First - 1;
1445 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
1451 Item (First .. Max),
1457 -- Exit when all or zero data received. Zero means that the socket
1460 exit when Index < First or else Index = Max;
1471 (Stream : in out Stream_Socket_Stream_Type;
1472 Item : out Ada.Streams.Stream_Element_Array;
1473 Last : out Ada.Streams.Stream_Element_Offset)
1475 First : Ada.Streams.Stream_Element_Offset := Item'First;
1476 Index : Ada.Streams.Stream_Element_Offset := First - 1;
1477 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
1481 Receive_Socket (Stream.Socket, Item (First .. Max), Index);
1484 -- Exit when all or zero data received. Zero means that the socket
1487 exit when Index < First or else Index = Max;
1493 --------------------
1494 -- Receive_Socket --
1495 --------------------
1497 procedure Receive_Socket
1498 (Socket : Socket_Type;
1499 Item : out Ada.Streams.Stream_Element_Array;
1500 Last : out Ada.Streams.Stream_Element_Offset;
1501 Flags : Request_Flag_Type := No_Request_Flag)
1503 use type Ada.Streams.Stream_Element_Offset;
1510 Item (Item'First)'Address,
1514 if Res = Failure then
1515 Raise_Socket_Error (Socket_Errno);
1518 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1521 --------------------
1522 -- Receive_Socket --
1523 --------------------
1525 procedure Receive_Socket
1526 (Socket : Socket_Type;
1527 Item : out Ada.Streams.Stream_Element_Array;
1528 Last : out Ada.Streams.Stream_Element_Offset;
1529 From : out Sock_Addr_Type;
1530 Flags : Request_Flag_Type := No_Request_Flag)
1532 use type Ada.Streams.Stream_Element_Offset;
1535 Sin : aliased Sockaddr_In;
1536 Len : aliased C.int := Sin'Size / 8;
1542 Item (Item'First)'Address,
1545 Sin'Unchecked_Access,
1546 Len'Unchecked_Access);
1548 if Res = Failure then
1549 Raise_Socket_Error (Socket_Errno);
1552 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1554 From.Addr := To_Inet_Addr (Sin.Sin_Addr);
1555 From.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
1562 function Resolve_Error
1563 (Error_Value : Integer;
1564 From_Errno : Boolean := True) return Error_Type
1566 use GNAT.Sockets.Constants;
1569 if not From_Errno then
1571 when Constants.HOST_NOT_FOUND => return Unknown_Host;
1572 when Constants.TRY_AGAIN => return Host_Name_Lookup_Failure;
1573 when Constants.NO_RECOVERY =>
1574 return Non_Recoverable_Error;
1575 when Constants.NO_DATA => return Unknown_Server_Error;
1576 when others => return Cannot_Resolve_Error;
1581 when ENOERROR => return Success;
1582 when EACCES => return Permission_Denied;
1583 when EADDRINUSE => return Address_Already_In_Use;
1584 when EADDRNOTAVAIL => return Cannot_Assign_Requested_Address;
1585 when EAFNOSUPPORT =>
1586 return Address_Family_Not_Supported_By_Protocol;
1587 when EALREADY => return Operation_Already_In_Progress;
1588 when EBADF => return Bad_File_Descriptor;
1589 when ECONNABORTED => return Software_Caused_Connection_Abort;
1590 when ECONNREFUSED => return Connection_Refused;
1591 when ECONNRESET => return Connection_Reset_By_Peer;
1592 when EDESTADDRREQ => return Destination_Address_Required;
1593 when EFAULT => return Bad_Address;
1594 when EHOSTDOWN => return Host_Is_Down;
1595 when EHOSTUNREACH => return No_Route_To_Host;
1596 when EINPROGRESS => return Operation_Now_In_Progress;
1597 when EINTR => return Interrupted_System_Call;
1598 when EINVAL => return Invalid_Argument;
1599 when EIO => return Input_Output_Error;
1600 when EISCONN => return Transport_Endpoint_Already_Connected;
1601 when ELOOP => return Too_Many_Symbolic_Links;
1602 when EMFILE => return Too_Many_Open_Files;
1603 when EMSGSIZE => return Message_Too_Long;
1604 when ENAMETOOLONG => return File_Name_Too_Long;
1605 when ENETDOWN => return Network_Is_Down;
1607 return Network_Dropped_Connection_Because_Of_Reset;
1608 when ENETUNREACH => return Network_Is_Unreachable;
1609 when ENOBUFS => return No_Buffer_Space_Available;
1610 when ENOPROTOOPT => return Protocol_Not_Available;
1611 when ENOTCONN => return Transport_Endpoint_Not_Connected;
1612 when ENOTSOCK => return Socket_Operation_On_Non_Socket;
1613 when EOPNOTSUPP => return Operation_Not_Supported;
1614 when EPFNOSUPPORT => return Protocol_Family_Not_Supported;
1615 when EPROTONOSUPPORT => return Protocol_Not_Supported;
1616 when EPROTOTYPE => return Protocol_Wrong_Type_For_Socket;
1618 return Cannot_Send_After_Transport_Endpoint_Shutdown;
1619 when ESOCKTNOSUPPORT => return Socket_Type_Not_Supported;
1620 when ETIMEDOUT => return Connection_Timed_Out;
1621 when ETOOMANYREFS => return Too_Many_References;
1622 when EWOULDBLOCK => return Resource_Temporarily_Unavailable;
1623 when others => null;
1626 return Cannot_Resolve_Error;
1629 -----------------------
1630 -- Resolve_Exception --
1631 -----------------------
1633 function Resolve_Exception
1634 (Occurrence : Exception_Occurrence) return Error_Type
1636 Id : constant Exception_Id := Exception_Identity (Occurrence);
1637 Msg : constant String := Exception_Message (Occurrence);
1638 First : Natural := Msg'First;
1643 while First <= Msg'Last
1644 and then Msg (First) not in '0' .. '9'
1649 if First > Msg'Last then
1650 return Cannot_Resolve_Error;
1655 while Last < Msg'Last
1656 and then Msg (Last + 1) in '0' .. '9'
1661 Val := Integer'Value (Msg (First .. Last));
1663 if Id = Socket_Error_Id then
1664 return Resolve_Error (Val);
1665 elsif Id = Host_Error_Id then
1666 return Resolve_Error (Val, False);
1668 return Cannot_Resolve_Error;
1670 end Resolve_Exception;
1672 --------------------
1673 -- Receive_Vector --
1674 --------------------
1676 procedure Receive_Vector
1677 (Socket : Socket_Type;
1678 Vector : Vector_Type;
1679 Count : out Ada.Streams.Stream_Element_Count)
1687 Vector (Vector'First)'Address,
1690 if Res = Failure then
1691 Raise_Socket_Error (Socket_Errno);
1694 Count := Ada.Streams.Stream_Element_Count (Res);
1701 procedure Send_Socket
1702 (Socket : Socket_Type;
1703 Item : Ada.Streams.Stream_Element_Array;
1704 Last : out Ada.Streams.Stream_Element_Offset;
1705 Flags : Request_Flag_Type := No_Request_Flag)
1707 use type Ada.Streams.Stream_Element_Offset;
1715 Item (Item'First)'Address,
1717 Set_Forced_Flags (To_Int (Flags)));
1719 if Res = Failure then
1720 Raise_Socket_Error (Socket_Errno);
1723 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1730 procedure Send_Socket
1731 (Socket : Socket_Type;
1732 Item : Ada.Streams.Stream_Element_Array;
1733 Last : out Ada.Streams.Stream_Element_Offset;
1734 To : Sock_Addr_Type;
1735 Flags : Request_Flag_Type := No_Request_Flag)
1737 use type Ada.Streams.Stream_Element_Offset;
1740 Sin : aliased Sockaddr_In;
1741 Len : constant C.int := Sin'Size / 8;
1744 Set_Length (Sin'Unchecked_Access, Len);
1745 Set_Family (Sin'Unchecked_Access, Families (To.Family));
1746 Set_Address (Sin'Unchecked_Access, To_In_Addr (To.Addr));
1748 (Sin'Unchecked_Access,
1749 Short_To_Network (C.unsigned_short (To.Port)));
1753 Item (Item'First)'Address,
1755 Set_Forced_Flags (To_Int (Flags)),
1756 Sin'Unchecked_Access,
1759 if Res = Failure then
1760 Raise_Socket_Error (Socket_Errno);
1763 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1770 procedure Send_Vector
1771 (Socket : Socket_Type;
1772 Vector : Vector_Type;
1773 Count : out Ada.Streams.Stream_Element_Count)
1781 Vector (Vector'First)'Address,
1784 if Res = Failure then
1785 Raise_Socket_Error (Socket_Errno);
1788 Count := Ada.Streams.Stream_Element_Count (Res);
1795 procedure Set (Item : in out Socket_Set_Type; Socket : Socket_Type) is
1797 if Item.Set = No_Socket_Set then
1798 Item.Set := New_Socket_Set (No_Socket_Set);
1799 Item.Last := Socket;
1801 elsif Item.Last < Socket then
1802 Item.Last := Socket;
1805 Insert_Socket_In_Set (Item.Set, C.int (Socket));
1808 ----------------------
1809 -- Set_Forced_Flags --
1810 ----------------------
1812 function Set_Forced_Flags (F : C.int) return C.int is
1813 use type C.unsigned;
1814 function To_unsigned is
1815 new Ada.Unchecked_Conversion (C.int, C.unsigned);
1817 new Ada.Unchecked_Conversion (C.unsigned, C.int);
1819 return To_int (To_unsigned (F) or Constants.MSG_Forced_Flags);
1820 end Set_Forced_Flags;
1822 -----------------------
1823 -- Set_Socket_Option --
1824 -----------------------
1826 procedure Set_Socket_Option
1827 (Socket : Socket_Type;
1828 Level : Level_Type := Socket_Level;
1829 Option : Option_Type)
1831 V8 : aliased Two_Int;
1833 V1 : aliased C.unsigned_char;
1834 Len : aliased C.int;
1835 Add : System.Address := Null_Address;
1844 V4 := C.int (Boolean'Pos (Option.Enabled));
1849 V8 (V8'First) := C.int (Boolean'Pos (Option.Enabled));
1850 V8 (V8'Last) := C.int (Option.Seconds);
1856 V4 := C.int (Option.Size);
1861 V4 := C.int (Boolean'Pos (True));
1865 when Add_Membership |
1867 V8 (V8'First) := To_Int (To_In_Addr (Option.Multicast_Address));
1868 V8 (V8'Last) := To_Int (To_In_Addr (Option.Local_Interface));
1872 when Multicast_TTL =>
1873 V1 := C.unsigned_char (Option.Time_To_Live);
1877 when Multicast_Loop =>
1878 V1 := C.unsigned_char (Boolean'Pos (Option.Enabled));
1887 Options (Option.Name),
1890 if Res = Failure then
1891 Raise_Socket_Error (Socket_Errno);
1893 end Set_Socket_Option;
1895 ----------------------
1896 -- Short_To_Network --
1897 ----------------------
1899 function Short_To_Network (S : C.unsigned_short) return C.unsigned_short is
1900 use type C.unsigned_short;
1903 -- Big-endian case. No conversion needed. On these platforms,
1904 -- htons() defaults to a null procedure.
1906 pragma Warnings (Off);
1907 -- Since the test can generate "always True/False" warning
1909 if Default_Bit_Order = High_Order_First then
1912 pragma Warnings (On);
1914 -- Little-endian case. We must swap the high and low bytes of this
1915 -- short to make the port number network compliant.
1918 return (S / 256) + (S mod 256) * 256;
1920 end Short_To_Network;
1922 ---------------------
1923 -- Shutdown_Socket --
1924 ---------------------
1926 procedure Shutdown_Socket
1927 (Socket : Socket_Type;
1928 How : Shutmode_Type := Shut_Read_Write)
1933 Res := C_Shutdown (C.int (Socket), Shutmodes (How));
1935 if Res = Failure then
1936 Raise_Socket_Error (Socket_Errno);
1938 end Shutdown_Socket;
1945 (Socket : Socket_Type;
1946 Send_To : Sock_Addr_Type) return Stream_Access
1948 S : Datagram_Socket_Stream_Access;
1951 S := new Datagram_Socket_Stream_Type;
1954 S.From := Get_Socket_Name (Socket);
1955 return Stream_Access (S);
1962 function Stream (Socket : Socket_Type) return Stream_Access is
1963 S : Stream_Socket_Stream_Access;
1965 S := new Stream_Socket_Stream_Type;
1967 return Stream_Access (S);
1974 function To_C (Socket : Socket_Type) return Integer is
1976 return Integer (Socket);
1983 function To_Host_Entry (E : Hostent) return Host_Entry_Type is
1986 Official : constant String :=
1987 C.Strings.Value (E.H_Name);
1989 Aliases : constant Chars_Ptr_Array :=
1990 Chars_Ptr_Pointers.Value (E.H_Aliases);
1991 -- H_Aliases points to a list of name aliases. The list is terminated by
1994 Addresses : constant In_Addr_Access_Array :=
1995 In_Addr_Access_Pointers.Value (E.H_Addr_List);
1996 -- H_Addr_List points to a list of binary addresses (in network byte
1997 -- order). The list is terminated by a NULL pointer.
1999 -- H_Length is not used because it is currently only set to 4.
2000 -- H_Addrtype is always AF_INET
2002 Result : Host_Entry_Type
2003 (Aliases_Length => Aliases'Length - 1,
2004 Addresses_Length => Addresses'Length - 1);
2005 -- The last element is a null pointer
2011 Result.Official := To_Name (Official);
2013 Source := Aliases'First;
2014 Target := Result.Aliases'First;
2015 while Target <= Result.Aliases_Length loop
2016 Result.Aliases (Target) :=
2017 To_Name (C.Strings.Value (Aliases (Source)));
2018 Source := Source + 1;
2019 Target := Target + 1;
2022 Source := Addresses'First;
2023 Target := Result.Addresses'First;
2024 while Target <= Result.Addresses_Length loop
2025 Result.Addresses (Target) :=
2026 To_Inet_Addr (Addresses (Source).all);
2027 Source := Source + 1;
2028 Target := Target + 1;
2038 function To_In_Addr (Addr : Inet_Addr_Type) return Thin.In_Addr is
2040 if Addr.Family = Family_Inet then
2041 return (S_B1 => C.unsigned_char (Addr.Sin_V4 (1)),
2042 S_B2 => C.unsigned_char (Addr.Sin_V4 (2)),
2043 S_B3 => C.unsigned_char (Addr.Sin_V4 (3)),
2044 S_B4 => C.unsigned_char (Addr.Sin_V4 (4)));
2054 function To_Inet_Addr
2055 (Addr : In_Addr) return Inet_Addr_Type
2057 Result : Inet_Addr_Type;
2059 Result.Sin_V4 (1) := Inet_Addr_Comp_Type (Addr.S_B1);
2060 Result.Sin_V4 (2) := Inet_Addr_Comp_Type (Addr.S_B2);
2061 Result.Sin_V4 (3) := Inet_Addr_Comp_Type (Addr.S_B3);
2062 Result.Sin_V4 (4) := Inet_Addr_Comp_Type (Addr.S_B4);
2070 function To_Int (F : Request_Flag_Type) return C.int
2072 Current : Request_Flag_Type := F;
2073 Result : C.int := 0;
2076 for J in Flags'Range loop
2077 exit when Current = 0;
2079 if Current mod 2 /= 0 then
2080 if Flags (J) = -1 then
2081 Raise_Socket_Error (Constants.EOPNOTSUPP);
2083 Result := Result + Flags (J);
2086 Current := Current / 2;
2096 function To_Name (N : String) return Name_Type is
2098 return Name_Type'(N'Length, N);
2101 ----------------------
2102 -- To_Service_Entry --
2103 ----------------------
2105 function To_Service_Entry (E : Servent) return Service_Entry_Type is
2108 Official : constant String :=
2109 C.Strings.Value (E.S_Name);
2111 Aliases : constant Chars_Ptr_Array :=
2112 Chars_Ptr_Pointers.Value (E.S_Aliases);
2113 -- S_Aliases points to a list of name aliases. The list is
2114 -- terminated by a NULL pointer.
2116 Protocol : constant String :=
2117 C.Strings.Value (E.S_Proto);
2119 Result : Service_Entry_Type
2120 (Aliases_Length => Aliases'Length - 1);
2121 -- The last element is a null pointer
2127 Result.Official := To_Name (Official);
2129 Source := Aliases'First;
2130 Target := Result.Aliases'First;
2131 while Target <= Result.Aliases_Length loop
2132 Result.Aliases (Target) :=
2133 To_Name (C.Strings.Value (Aliases (Source)));
2134 Source := Source + 1;
2135 Target := Target + 1;
2139 Port_Type (Network_To_Short (C.unsigned_short (E.S_Port)));
2141 Result.Protocol := To_Name (Protocol);
2144 end To_Service_Entry;
2150 function To_String (HN : Name_Type) return String is
2152 return HN.Name (1 .. HN.Length);
2159 function To_Timeval (Val : Selector_Duration) return Timeval is
2164 -- If zero, set result as zero (otherwise it gets rounded down to -1)
2170 -- Normal case where we do round down
2173 S := Timeval_Unit (Val - 0.5);
2174 MS := Timeval_Unit (1_000_000 * (Val - Selector_Duration (S)));
2185 (Stream : in out Datagram_Socket_Stream_Type;
2186 Item : Ada.Streams.Stream_Element_Array)
2188 First : Ada.Streams.Stream_Element_Offset := Item'First;
2189 Index : Ada.Streams.Stream_Element_Offset := First - 1;
2190 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
2196 Item (First .. Max),
2200 -- Exit when all or zero data sent. Zero means that the socket has
2201 -- been closed by peer.
2203 exit when Index < First or else Index = Max;
2208 if Index /= Max then
2218 (Stream : in out Stream_Socket_Stream_Type;
2219 Item : Ada.Streams.Stream_Element_Array)
2221 First : Ada.Streams.Stream_Element_Offset := Item'First;
2222 Index : Ada.Streams.Stream_Element_Offset := First - 1;
2223 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
2227 Send_Socket (Stream.Socket, Item (First .. Max), Index);
2229 -- Exit when all or zero data sent. Zero means that the socket has
2230 -- been closed by peer.
2232 exit when Index < First or else Index = Max;
2237 if Index /= Max then