1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S --
9 -- Copyright (C) 2001-2005, 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.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_If => Constants.IP_MULTICAST_IF,
96 Multicast_TTL => Constants.IP_MULTICAST_TTL,
97 Multicast_Loop => Constants.IP_MULTICAST_LOOP,
98 Send_Timeout => Constants.SO_SNDTIMEO,
99 Receive_Timeout => Constants.SO_RCVTIMEO);
101 Flags : constant array (0 .. 3) of C.int :=
102 (0 => Constants.MSG_OOB, -- Process_Out_Of_Band_Data
103 1 => Constants.MSG_PEEK, -- Peek_At_Incoming_Data
104 2 => Constants.MSG_WAITALL, -- Wait_For_A_Full_Reception
105 3 => Constants.MSG_EOR); -- Send_End_Of_Record
107 Socket_Error_Id : constant Exception_Id := Socket_Error'Identity;
108 Host_Error_Id : constant Exception_Id := Host_Error'Identity;
110 Hex_To_Char : constant String (1 .. 16) := "0123456789ABCDEF";
111 -- Use to print in hexadecimal format
113 function To_In_Addr is new Ada.Unchecked_Conversion (C.int, In_Addr);
114 function To_Int is new Ada.Unchecked_Conversion (In_Addr, C.int);
116 function Err_Code_Image (E : Integer) return String;
117 -- Return the value of E surrounded with brackets
119 -----------------------
120 -- Local subprograms --
121 -----------------------
123 function Resolve_Error
124 (Error_Value : Integer;
125 From_Errno : Boolean := True) return Error_Type;
126 -- Associate an enumeration value (error_type) to en error value (errno).
127 -- From_Errno prevents from mixing h_errno with errno.
129 function To_Name (N : String) return Name_Type;
130 function To_String (HN : Name_Type) return String;
131 -- Conversion functions
133 function To_Int (F : Request_Flag_Type) return C.int;
134 -- Return the int value corresponding to the specified flags combination
136 function Set_Forced_Flags (F : C.int) return C.int;
137 -- Return F with the bits from Constants.MSG_Forced_Flags forced set
139 function Short_To_Network
140 (S : C.unsigned_short) return C.unsigned_short;
141 pragma Inline (Short_To_Network);
142 -- Convert a port number into a network port number
144 function Network_To_Short
145 (S : C.unsigned_short) return C.unsigned_short
146 renames Short_To_Network;
147 -- Symetric operation
150 (Val : Inet_Addr_VN_Type;
151 Hex : Boolean := False) return String;
152 -- Output an array of inet address components in hex or decimal mode
154 function Is_IP_Address (Name : String) return Boolean;
155 -- Return true when Name is an IP address in standard dot notation
157 function To_In_Addr (Addr : Inet_Addr_Type) return Thin.In_Addr;
158 procedure To_Inet_Addr
160 Result : out Inet_Addr_Type);
161 -- Conversion functions
163 function To_Host_Entry (E : Hostent) return Host_Entry_Type;
164 -- Conversion function
166 function To_Service_Entry (E : Servent) return Service_Entry_Type;
167 -- Conversion function
169 function To_Timeval (Val : Timeval_Duration) return Timeval;
170 -- Separate Val in seconds and microseconds
172 function To_Duration (Val : Timeval) return Timeval_Duration;
173 -- Reconstruct a Duration value from a Timeval record (seconds and
176 procedure Raise_Socket_Error (Error : Integer);
177 -- Raise Socket_Error with an exception message describing the error code
180 procedure Raise_Host_Error (H_Error : Integer);
181 -- Raise Host_Error exception with message describing error code (note
182 -- hstrerror seems to be obsolete) from h_errno.
184 procedure Narrow (Item : in out Socket_Set_Type);
185 -- Update Last as it may be greater than the real last socket
187 -- Types needed for Datagram_Socket_Stream_Type
189 type Datagram_Socket_Stream_Type is new Root_Stream_Type with record
190 Socket : Socket_Type;
192 From : Sock_Addr_Type;
195 type Datagram_Socket_Stream_Access is
196 access all Datagram_Socket_Stream_Type;
199 (Stream : in out Datagram_Socket_Stream_Type;
200 Item : out Ada.Streams.Stream_Element_Array;
201 Last : out Ada.Streams.Stream_Element_Offset);
204 (Stream : in out Datagram_Socket_Stream_Type;
205 Item : Ada.Streams.Stream_Element_Array);
207 -- Types needed for Stream_Socket_Stream_Type
209 type Stream_Socket_Stream_Type is new Root_Stream_Type with record
210 Socket : Socket_Type;
213 type Stream_Socket_Stream_Access is
214 access all Stream_Socket_Stream_Type;
217 (Stream : in out Stream_Socket_Stream_Type;
218 Item : out Ada.Streams.Stream_Element_Array;
219 Last : out Ada.Streams.Stream_Element_Offset);
222 (Stream : in out Stream_Socket_Stream_Type;
223 Item : Ada.Streams.Stream_Element_Array);
229 function "+" (L, R : Request_Flag_Type) return Request_Flag_Type is
238 procedure Abort_Selector (Selector : Selector_Type) is
239 Buf : aliased Character := ASCII.NUL;
243 -- Send an empty array to unblock C select system call
245 Res := C_Send (C.int (Selector.W_Sig_Socket), Buf'Address, 1,
246 Constants.MSG_Forced_Flags);
247 if Res = Failure then
248 Raise_Socket_Error (Socket_Errno);
256 procedure Accept_Socket
257 (Server : Socket_Type;
258 Socket : out Socket_Type;
259 Address : out Sock_Addr_Type)
262 Sin : aliased Sockaddr_In;
263 Len : aliased C.int := Sin'Size / 8;
266 Res := C_Accept (C.int (Server), Sin'Address, Len'Access);
268 if Res = Failure then
269 Raise_Socket_Error (Socket_Errno);
272 Socket := Socket_Type (Res);
274 To_Inet_Addr (Sin.Sin_Addr, Address.Addr);
275 Address.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
283 (E : Host_Entry_Type;
284 N : Positive := 1) return Inet_Addr_Type
287 return E.Addresses (N);
290 ----------------------
291 -- Addresses_Length --
292 ----------------------
294 function Addresses_Length (E : Host_Entry_Type) return Natural is
296 return E.Addresses_Length;
297 end Addresses_Length;
304 (E : Host_Entry_Type;
305 N : Positive := 1) return String
308 return To_String (E.Aliases (N));
316 (S : Service_Entry_Type;
317 N : Positive := 1) return String
320 return To_String (S.Aliases (N));
327 function Aliases_Length (E : Host_Entry_Type) return Natural is
329 return E.Aliases_Length;
336 function Aliases_Length (S : Service_Entry_Type) return Natural is
338 return S.Aliases_Length;
345 procedure Bind_Socket
346 (Socket : Socket_Type;
347 Address : Sock_Addr_Type)
350 Sin : aliased Sockaddr_In;
351 Len : constant C.int := Sin'Size / 8;
354 if Address.Family = Family_Inet6 then
358 Set_Length (Sin'Unchecked_Access, Len);
359 Set_Family (Sin'Unchecked_Access, Families (Address.Family));
361 (Sin'Unchecked_Access,
362 Short_To_Network (C.unsigned_short (Address.Port)));
364 Res := C_Bind (C.int (Socket), Sin'Address, Len);
366 if Res = Failure then
367 Raise_Socket_Error (Socket_Errno);
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 Status : out Selector_Status;
380 Timeout : Selector_Duration := Forever)
382 E_Socket_Set : Socket_Set_Type; -- (No_Socket, No_Socket_Set)
385 (Selector, R_Socket_Set, W_Socket_Set, E_Socket_Set, Status, Timeout);
388 procedure Check_Selector
389 (Selector : in out Selector_Type;
390 R_Socket_Set : in out Socket_Set_Type;
391 W_Socket_Set : in out Socket_Set_Type;
392 E_Socket_Set : in out Socket_Set_Type;
393 Status : out Selector_Status;
394 Timeout : Selector_Duration := Forever)
398 RSig : Socket_Type renames Selector.R_Sig_Socket;
399 RSet : Socket_Set_Type;
400 WSet : Socket_Set_Type;
401 ESet : Socket_Set_Type;
402 TVal : aliased Timeval;
403 TPtr : Timeval_Access;
409 -- No timeout or Forever is indicated by a null timeval pointer
411 if Timeout = Forever then
414 TVal := To_Timeval (Timeout);
415 TPtr := TVal'Unchecked_Access;
418 -- Copy R_Socket_Set in RSet and add read signalling socket
420 RSet := (Set => New_Socket_Set (R_Socket_Set.Set),
421 Last => R_Socket_Set.Last);
424 -- Copy W_Socket_Set in WSet
426 WSet := (Set => New_Socket_Set (W_Socket_Set.Set),
427 Last => W_Socket_Set.Last);
429 -- Copy E_Socket_Set in ESet
431 ESet := (Set => New_Socket_Set (E_Socket_Set.Set),
432 Last => E_Socket_Set.Last);
434 Last := C.int'Max (C.int'Max (C.int (RSet.Last),
446 if Res = Failure then
447 Raise_Socket_Error (Socket_Errno);
450 -- If Select was resumed because of read signalling socket, read this
451 -- data and remove socket from set.
453 if Is_Set (RSet, RSig) then
460 Res := C_Recv (C.int (RSig), Buf'Address, 1, 0);
462 if Res = Failure then
463 Raise_Socket_Error (Socket_Errno);
473 -- Update RSet, WSet and ESet in regard to their new socket sets
479 -- Reset RSet as it should be if R_Sig_Socket was not added
481 if Is_Empty (RSet) then
485 if Is_Empty (WSet) then
489 if Is_Empty (ESet) then
493 -- Deliver RSet, WSet and ESet
495 Empty (R_Socket_Set);
496 R_Socket_Set := RSet;
498 Empty (W_Socket_Set);
499 W_Socket_Set := WSet;
501 Empty (E_Socket_Set);
502 E_Socket_Set := ESet;
508 -- The local socket sets must be emptied before propagating
509 -- Socket_Error so the associated storage is freed.
523 (Item : in out Socket_Set_Type;
524 Socket : Socket_Type)
526 Last : aliased C.int := C.int (Item.Last);
528 if Item.Last /= No_Socket then
529 Remove_Socket_From_Set (Item.Set, C.int (Socket));
530 Last_Socket_In_Set (Item.Set, Last'Unchecked_Access);
531 Item.Last := Socket_Type (Last);
539 procedure Close_Selector (Selector : in out Selector_Type) is
542 -- Close the signalling sockets used internally for the implementation
543 -- of Abort_Selector. Exceptions are ignored because these sockets
544 -- are implementation artefacts of no interest to the user, and
545 -- there is little that can be done if either Close_Socket call fails
546 -- (which theoretically should not happen anyway). We also want to try
547 -- to perform the second Close_Socket even if the first one failed.
550 Close_Socket (Selector.R_Sig_Socket);
557 Close_Socket (Selector.W_Sig_Socket);
568 procedure Close_Socket (Socket : Socket_Type) is
572 Res := C_Close (C.int (Socket));
574 if Res = Failure then
575 Raise_Socket_Error (Socket_Errno);
583 procedure Connect_Socket
584 (Socket : Socket_Type;
585 Server : in out Sock_Addr_Type)
588 Sin : aliased Sockaddr_In;
589 Len : constant C.int := Sin'Size / 8;
592 if Server.Family = Family_Inet6 then
596 Set_Length (Sin'Unchecked_Access, Len);
597 Set_Family (Sin'Unchecked_Access, Families (Server.Family));
598 Set_Address (Sin'Unchecked_Access, To_In_Addr (Server.Addr));
600 (Sin'Unchecked_Access,
601 Short_To_Network (C.unsigned_short (Server.Port)));
603 Res := C_Connect (C.int (Socket), Sin'Address, Len);
605 if Res = Failure then
606 Raise_Socket_Error (Socket_Errno);
614 procedure Control_Socket
615 (Socket : Socket_Type;
616 Request : in out Request_Type)
623 when Non_Blocking_IO =>
624 Arg := C.int (Boolean'Pos (Request.Enabled));
626 when N_Bytes_To_Read =>
633 Requests (Request.Name),
634 Arg'Unchecked_Access);
636 if Res = Failure then
637 Raise_Socket_Error (Socket_Errno);
641 when Non_Blocking_IO =>
644 when N_Bytes_To_Read =>
645 Request.Size := Natural (Arg);
654 (Source : Socket_Set_Type;
655 Target : in out Socket_Set_Type)
659 if Source.Last /= No_Socket then
660 Target.Set := New_Socket_Set (Source.Set);
661 Target.Last := Source.Last;
665 ---------------------
666 -- Create_Selector --
667 ---------------------
669 procedure Create_Selector (Selector : out Selector_Type) is
674 Sin : aliased Sockaddr_In;
675 Len : aliased C.int := Sin'Size / 8;
679 -- We open two signalling sockets. One of them is used to send data to
680 -- the other, which is included in a C_Select socket set. The
681 -- communication is used to force the call to C_Select to complete, and
682 -- the waiting task to resume its execution.
684 -- Create a listening socket
686 S0 := C_Socket (Constants.AF_INET, Constants.SOCK_STREAM, 0);
689 Raise_Socket_Error (Socket_Errno);
692 -- Bind the socket to any unused port on localhost
694 Sin.Sin_Addr.S_B1 := 127;
695 Sin.Sin_Addr.S_B2 := 0;
696 Sin.Sin_Addr.S_B3 := 0;
697 Sin.Sin_Addr.S_B4 := 1;
700 Res := C_Bind (S0, Sin'Address, Len);
702 if Res = Failure then
705 Raise_Socket_Error (Err);
708 -- Get the port used by the socket
710 Res := C_Getsockname (S0, Sin'Address, Len'Access);
712 if Res = Failure then
715 Raise_Socket_Error (Err);
718 -- Set backlog to 1 to guarantee that exactly one call to connect(2)
721 Res := C_Listen (S0, 1);
723 if Res = Failure then
726 Raise_Socket_Error (Err);
729 S1 := C_Socket (Constants.AF_INET, Constants.SOCK_STREAM, 0);
734 Raise_Socket_Error (Err);
737 -- Do a connect and accept the connection
739 Res := C_Connect (S1, Sin'Address, Len);
741 if Res = Failure then
745 Raise_Socket_Error (Err);
748 -- Since the call to connect(2) has suceeded and the backlog limit on
749 -- the listening socket is 1, we know that there is now exactly one
750 -- pending connection on S0, which is the one from S1.
752 S2 := C_Accept (S0, Sin'Address, Len'Access);
758 Raise_Socket_Error (Err);
763 if Res = Failure then
764 Raise_Socket_Error (Socket_Errno);
767 Selector.R_Sig_Socket := Socket_Type (S1);
768 Selector.W_Sig_Socket := Socket_Type (S2);
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_Socket_Set then
799 Free_Socket_Set (Item.Set);
800 Item.Set := No_Socket_Set;
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);
857 function Get_Address (Stream : Stream_Access) return Sock_Addr_Type is
859 if Stream = null then
861 elsif 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 Res : Hostent_Access;
883 -- This C function is not always thread-safe. Protect against
884 -- concurrent access.
887 Res := C_Gethostbyaddr (HA'Address, HA'Size / 8, Constants.AF_INET);
892 Raise_Host_Error (Err);
895 -- Translate from the C format to the API format
898 HE : constant Host_Entry_Type := To_Host_Entry (Res.all);
904 end Get_Host_By_Address;
906 ----------------------
907 -- Get_Host_By_Name --
908 ----------------------
910 function Get_Host_By_Name (Name : String) return Host_Entry_Type is
911 HN : constant C.char_array := C.To_C (Name);
912 Res : Hostent_Access;
916 -- Detect IP address name and redirect to Inet_Addr
918 if Is_IP_Address (Name) then
919 return Get_Host_By_Address (Inet_Addr (Name));
922 -- This C function is not always thread-safe. Protect against
923 -- concurrent access.
926 Res := C_Gethostbyname (HN);
931 Raise_Host_Error (Err);
934 -- Translate from the C format to the API format
937 HE : constant Host_Entry_Type := To_Host_Entry (Res.all);
942 end Get_Host_By_Name;
948 function Get_Peer_Name (Socket : Socket_Type) return Sock_Addr_Type is
949 Sin : aliased Sockaddr_In;
950 Len : aliased C.int := Sin'Size / 8;
951 Res : Sock_Addr_Type (Family_Inet);
954 if C_Getpeername (C.int (Socket), Sin'Address, Len'Access) = Failure then
955 Raise_Socket_Error (Socket_Errno);
958 To_Inet_Addr (Sin.Sin_Addr, Res.Addr);
959 Res.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
964 -------------------------
965 -- Get_Service_By_Name --
966 -------------------------
968 function Get_Service_By_Name
970 Protocol : String) return Service_Entry_Type
972 SN : constant C.char_array := C.To_C (Name);
973 SP : constant C.char_array := C.To_C (Protocol);
974 Res : Servent_Access;
977 -- This C function is not always thread-safe. Protect against
978 -- concurrent access.
981 Res := C_Getservbyname (SN, SP);
985 Ada.Exceptions.Raise_Exception
986 (Service_Error'Identity, "Service not found");
989 -- Translate from the C format to the API format
992 SE : constant Service_Entry_Type := To_Service_Entry (Res.all);
998 end Get_Service_By_Name;
1000 -------------------------
1001 -- Get_Service_By_Port --
1002 -------------------------
1004 function Get_Service_By_Port
1006 Protocol : String) return Service_Entry_Type
1008 SP : constant C.char_array := C.To_C (Protocol);
1009 Res : Servent_Access;
1012 -- This C function is not always thread-safe. Protect against
1013 -- concurrent access.
1016 Res := C_Getservbyport
1017 (C.int (Short_To_Network (C.unsigned_short (Port))), SP);
1021 Ada.Exceptions.Raise_Exception
1022 (Service_Error'Identity, "Service not found");
1025 -- Translate from the C format to the API format
1028 SE : constant Service_Entry_Type := To_Service_Entry (Res.all);
1034 end Get_Service_By_Port;
1036 ---------------------
1037 -- Get_Socket_Name --
1038 ---------------------
1040 function Get_Socket_Name
1041 (Socket : Socket_Type) return Sock_Addr_Type
1043 Sin : aliased Sockaddr_In;
1044 Len : aliased C.int := Sin'Size / 8;
1046 Addr : Sock_Addr_Type := No_Sock_Addr;
1049 Res := C_Getsockname (C.int (Socket), Sin'Address, Len'Access);
1050 if Res /= Failure then
1051 To_Inet_Addr (Sin.Sin_Addr, Addr.Addr);
1052 Addr.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
1056 end Get_Socket_Name;
1058 -----------------------
1059 -- Get_Socket_Option --
1060 -----------------------
1062 function Get_Socket_Option
1063 (Socket : Socket_Type;
1064 Level : Level_Type := Socket_Level;
1065 Name : Option_Name) return Option_Type
1067 use type C.unsigned_char;
1069 V8 : aliased Two_Int;
1071 V1 : aliased C.unsigned_char;
1072 VT : aliased Timeval;
1073 Len : aliased C.int;
1074 Add : System.Address;
1076 Opt : Option_Type (Name);
1080 when Multicast_Loop |
1114 Add, Len'Unchecked_Access);
1116 if Res = Failure then
1117 Raise_Socket_Error (Socket_Errno);
1125 Opt.Enabled := (V4 /= 0);
1128 Opt.Enabled := (V8 (V8'First) /= 0);
1129 Opt.Seconds := Natural (V8 (V8'Last));
1133 Opt.Size := Natural (V4);
1136 Opt.Error := Resolve_Error (Integer (V4));
1138 when Add_Membership |
1140 To_Inet_Addr (To_In_Addr (V8 (V8'First)), Opt.Multicast_Address);
1141 To_Inet_Addr (To_In_Addr (V8 (V8'Last)), Opt.Local_Interface);
1143 when Multicast_If =>
1144 To_Inet_Addr (To_In_Addr (V4), Opt.Outgoing_If);
1146 when Multicast_TTL =>
1147 Opt.Time_To_Live := Integer (V1);
1149 when Multicast_Loop =>
1150 Opt.Enabled := (V1 /= 0);
1154 Opt.Timeout := To_Duration (VT);
1159 end Get_Socket_Option;
1165 function Host_Name return String is
1166 Name : aliased C.char_array (1 .. 64);
1170 Res := C_Gethostname (Name'Address, Name'Length);
1172 if Res = Failure then
1173 Raise_Socket_Error (Socket_Errno);
1176 return C.To_Ada (Name);
1184 (Val : Inet_Addr_VN_Type;
1185 Hex : Boolean := False) return String
1187 -- The largest Inet_Addr_Comp_Type image occurs with IPv4. It
1188 -- has at most a length of 3 plus one '.' character.
1190 Buffer : String (1 .. 4 * Val'Length);
1191 Length : Natural := 1;
1192 Separator : Character;
1194 procedure Img10 (V : Inet_Addr_Comp_Type);
1195 -- Append to Buffer image of V in decimal format
1197 procedure Img16 (V : Inet_Addr_Comp_Type);
1198 -- Append to Buffer image of V in hexadecimal format
1204 procedure Img10 (V : Inet_Addr_Comp_Type) is
1205 Img : constant String := V'Img;
1206 Len : constant Natural := Img'Length - 1;
1208 Buffer (Length .. Length + Len - 1) := Img (2 .. Img'Last);
1209 Length := Length + Len;
1216 procedure Img16 (V : Inet_Addr_Comp_Type) is
1218 Buffer (Length) := Hex_To_Char (Natural (V / 16) + 1);
1219 Buffer (Length + 1) := Hex_To_Char (Natural (V mod 16) + 1);
1220 Length := Length + 2;
1223 -- Start of processing for Image
1232 for J in Val'Range loop
1239 if J /= Val'Last then
1240 Buffer (Length) := Separator;
1241 Length := Length + 1;
1245 return Buffer (1 .. Length - 1);
1252 function Image (Value : Inet_Addr_Type) return String is
1254 if Value.Family = Family_Inet then
1255 return Image (Inet_Addr_VN_Type (Value.Sin_V4), Hex => False);
1257 return Image (Inet_Addr_VN_Type (Value.Sin_V6), Hex => True);
1265 function Image (Value : Sock_Addr_Type) return String is
1266 Port : constant String := Value.Port'Img;
1268 return Image (Value.Addr) & ':' & Port (2 .. Port'Last);
1275 function Image (Socket : Socket_Type) return String is
1284 function Inet_Addr (Image : String) return Inet_Addr_Type is
1285 use Interfaces.C.Strings;
1289 Result : Inet_Addr_Type;
1292 -- Special case for the all-ones broadcast address: this address
1293 -- has the same in_addr_t value as Failure, and thus cannot be
1294 -- properly returned by inet_addr(3).
1296 if Image = "255.255.255.255" then
1297 return Broadcast_Inet_Addr;
1299 -- Special case for an empty Image as on some platforms (e.g. Windows)
1300 -- calling Inet_Addr("") will not return an error.
1302 elsif Image = "" then
1303 Raise_Socket_Error (Constants.EINVAL);
1306 Img := New_String (Image);
1307 Res := C_Inet_Addr (Img);
1310 if Res = Failure then
1311 Raise_Socket_Error (Constants.EINVAL);
1314 To_Inet_Addr (To_In_Addr (Res), Result);
1322 procedure Initialize (Process_Blocking_IO : Boolean := False) is
1324 if not Initialized then
1325 Initialized := True;
1326 Thin.Initialize (Process_Blocking_IO);
1334 function Is_Empty (Item : Socket_Set_Type) return Boolean is
1336 return Item.Last = No_Socket;
1343 function Is_IP_Address (Name : String) return Boolean is
1345 for J in Name'Range loop
1347 and then Name (J) not in '0' .. '9'
1361 (Item : Socket_Set_Type;
1362 Socket : Socket_Type) return Boolean
1365 return Item.Last /= No_Socket
1366 and then Socket <= Item.Last
1367 and then Is_Socket_In_Set (Item.Set, C.int (Socket)) /= 0;
1374 procedure Listen_Socket
1375 (Socket : Socket_Type;
1376 Length : Positive := 15)
1378 Res : constant C.int := C_Listen (C.int (Socket), C.int (Length));
1380 if Res = Failure then
1381 Raise_Socket_Error (Socket_Errno);
1389 procedure Narrow (Item : in out Socket_Set_Type) is
1390 Last : aliased C.int := C.int (Item.Last);
1392 if Item.Set /= No_Socket_Set then
1393 Last_Socket_In_Set (Item.Set, Last'Unchecked_Access);
1394 Item.Last := Socket_Type (Last);
1402 function Official_Name (E : Host_Entry_Type) return String is
1404 return To_String (E.Official);
1411 function Official_Name (S : Service_Entry_Type) return String is
1413 return To_String (S.Official);
1420 function Port_Number (S : Service_Entry_Type) return Port_Type is
1429 function Protocol_Name (S : Service_Entry_Type) return String is
1431 return To_String (S.Protocol);
1434 ----------------------
1435 -- Raise_Host_Error --
1436 ----------------------
1438 procedure Raise_Host_Error (H_Error : Integer) is
1440 function Host_Error_Message return String;
1441 -- We do not use a C function like strerror because hstrerror that would
1442 -- correspond is obsolete. Return appropriate string for error value.
1444 ------------------------
1445 -- Host_Error_Message --
1446 ------------------------
1448 function Host_Error_Message return String is
1451 when Constants.HOST_NOT_FOUND => return "Host not found";
1452 when Constants.TRY_AGAIN => return "Try again";
1453 when Constants.NO_RECOVERY => return "No recovery";
1454 when Constants.NO_DATA => return "No address";
1455 when others => return "Unknown error";
1457 end Host_Error_Message;
1459 -- Start of processing for Raise_Host_Error
1462 Ada.Exceptions.Raise_Exception (Host_Error'Identity,
1463 Err_Code_Image (H_Error)
1464 & Host_Error_Message);
1465 end Raise_Host_Error;
1467 ------------------------
1468 -- Raise_Socket_Error --
1469 ------------------------
1471 procedure Raise_Socket_Error (Error : Integer) is
1472 use type C.Strings.chars_ptr;
1474 Ada.Exceptions.Raise_Exception (Socket_Error'Identity,
1475 Err_Code_Image (Error)
1476 & C.Strings.Value (Socket_Error_Message (Error)));
1477 end Raise_Socket_Error;
1484 (Stream : in out Datagram_Socket_Stream_Type;
1485 Item : out Ada.Streams.Stream_Element_Array;
1486 Last : out Ada.Streams.Stream_Element_Offset)
1488 First : Ada.Streams.Stream_Element_Offset := Item'First;
1489 Index : Ada.Streams.Stream_Element_Offset := First - 1;
1490 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
1496 Item (First .. Max),
1502 -- Exit when all or zero data received. Zero means that the socket
1505 exit when Index < First or else Index = Max;
1516 (Stream : in out Stream_Socket_Stream_Type;
1517 Item : out Ada.Streams.Stream_Element_Array;
1518 Last : out Ada.Streams.Stream_Element_Offset)
1520 First : Ada.Streams.Stream_Element_Offset := Item'First;
1521 Index : Ada.Streams.Stream_Element_Offset := First - 1;
1522 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
1526 Receive_Socket (Stream.Socket, Item (First .. Max), Index);
1529 -- Exit when all or zero data received. Zero means that the socket
1532 exit when Index < First or else Index = Max;
1538 --------------------
1539 -- Receive_Socket --
1540 --------------------
1542 procedure Receive_Socket
1543 (Socket : Socket_Type;
1544 Item : out Ada.Streams.Stream_Element_Array;
1545 Last : out Ada.Streams.Stream_Element_Offset;
1546 Flags : Request_Flag_Type := No_Request_Flag)
1548 use type Ada.Streams.Stream_Element_Offset;
1555 Item (Item'First)'Address,
1559 if Res = Failure then
1560 Raise_Socket_Error (Socket_Errno);
1563 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1566 --------------------
1567 -- Receive_Socket --
1568 --------------------
1570 procedure Receive_Socket
1571 (Socket : Socket_Type;
1572 Item : out Ada.Streams.Stream_Element_Array;
1573 Last : out Ada.Streams.Stream_Element_Offset;
1574 From : out Sock_Addr_Type;
1575 Flags : Request_Flag_Type := No_Request_Flag)
1577 use type Ada.Streams.Stream_Element_Offset;
1580 Sin : aliased Sockaddr_In;
1581 Len : aliased C.int := Sin'Size / 8;
1587 Item (Item'First)'Address,
1590 Sin'Unchecked_Access,
1591 Len'Unchecked_Access);
1593 if Res = Failure then
1594 Raise_Socket_Error (Socket_Errno);
1597 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1599 To_Inet_Addr (Sin.Sin_Addr, From.Addr);
1600 From.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
1607 function Resolve_Error
1608 (Error_Value : Integer;
1609 From_Errno : Boolean := True) return Error_Type
1611 use GNAT.Sockets.Constants;
1614 if not From_Errno then
1616 when Constants.HOST_NOT_FOUND => return Unknown_Host;
1617 when Constants.TRY_AGAIN => return Host_Name_Lookup_Failure;
1618 when Constants.NO_RECOVERY =>
1619 return Non_Recoverable_Error;
1620 when Constants.NO_DATA => return Unknown_Server_Error;
1621 when others => return Cannot_Resolve_Error;
1626 when ENOERROR => return Success;
1627 when EACCES => return Permission_Denied;
1628 when EADDRINUSE => return Address_Already_In_Use;
1629 when EADDRNOTAVAIL => return Cannot_Assign_Requested_Address;
1630 when EAFNOSUPPORT =>
1631 return Address_Family_Not_Supported_By_Protocol;
1632 when EALREADY => return Operation_Already_In_Progress;
1633 when EBADF => return Bad_File_Descriptor;
1634 when ECONNABORTED => return Software_Caused_Connection_Abort;
1635 when ECONNREFUSED => return Connection_Refused;
1636 when ECONNRESET => return Connection_Reset_By_Peer;
1637 when EDESTADDRREQ => return Destination_Address_Required;
1638 when EFAULT => return Bad_Address;
1639 when EHOSTDOWN => return Host_Is_Down;
1640 when EHOSTUNREACH => return No_Route_To_Host;
1641 when EINPROGRESS => return Operation_Now_In_Progress;
1642 when EINTR => return Interrupted_System_Call;
1643 when EINVAL => return Invalid_Argument;
1644 when EIO => return Input_Output_Error;
1645 when EISCONN => return Transport_Endpoint_Already_Connected;
1646 when ELOOP => return Too_Many_Symbolic_Links;
1647 when EMFILE => return Too_Many_Open_Files;
1648 when EMSGSIZE => return Message_Too_Long;
1649 when ENAMETOOLONG => return File_Name_Too_Long;
1650 when ENETDOWN => return Network_Is_Down;
1652 return Network_Dropped_Connection_Because_Of_Reset;
1653 when ENETUNREACH => return Network_Is_Unreachable;
1654 when ENOBUFS => return No_Buffer_Space_Available;
1655 when ENOPROTOOPT => return Protocol_Not_Available;
1656 when ENOTCONN => return Transport_Endpoint_Not_Connected;
1657 when ENOTSOCK => return Socket_Operation_On_Non_Socket;
1658 when EOPNOTSUPP => return Operation_Not_Supported;
1659 when EPFNOSUPPORT => return Protocol_Family_Not_Supported;
1660 when EPROTONOSUPPORT => return Protocol_Not_Supported;
1661 when EPROTOTYPE => return Protocol_Wrong_Type_For_Socket;
1663 return Cannot_Send_After_Transport_Endpoint_Shutdown;
1664 when ESOCKTNOSUPPORT => return Socket_Type_Not_Supported;
1665 when ETIMEDOUT => return Connection_Timed_Out;
1666 when ETOOMANYREFS => return Too_Many_References;
1667 when EWOULDBLOCK => return Resource_Temporarily_Unavailable;
1668 when others => null;
1671 return Cannot_Resolve_Error;
1674 -----------------------
1675 -- Resolve_Exception --
1676 -----------------------
1678 function Resolve_Exception
1679 (Occurrence : Exception_Occurrence) return Error_Type
1681 Id : constant Exception_Id := Exception_Identity (Occurrence);
1682 Msg : constant String := Exception_Message (Occurrence);
1689 while First <= Msg'Last
1690 and then Msg (First) not in '0' .. '9'
1695 if First > Msg'Last then
1696 return Cannot_Resolve_Error;
1700 while Last < Msg'Last
1701 and then Msg (Last + 1) in '0' .. '9'
1706 Val := Integer'Value (Msg (First .. Last));
1708 if Id = Socket_Error_Id then
1709 return Resolve_Error (Val);
1710 elsif Id = Host_Error_Id then
1711 return Resolve_Error (Val, False);
1713 return Cannot_Resolve_Error;
1715 end Resolve_Exception;
1717 --------------------
1718 -- Receive_Vector --
1719 --------------------
1721 procedure Receive_Vector
1722 (Socket : Socket_Type;
1723 Vector : Vector_Type;
1724 Count : out Ada.Streams.Stream_Element_Count)
1732 Vector (Vector'First)'Address,
1735 if Res = Failure then
1736 Raise_Socket_Error (Socket_Errno);
1739 Count := Ada.Streams.Stream_Element_Count (Res);
1746 procedure Send_Socket
1747 (Socket : Socket_Type;
1748 Item : Ada.Streams.Stream_Element_Array;
1749 Last : out Ada.Streams.Stream_Element_Offset;
1750 Flags : Request_Flag_Type := No_Request_Flag)
1752 use type Ada.Streams.Stream_Element_Offset;
1760 Item (Item'First)'Address,
1762 Set_Forced_Flags (To_Int (Flags)));
1764 if Res = Failure then
1765 Raise_Socket_Error (Socket_Errno);
1768 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1775 procedure Send_Socket
1776 (Socket : Socket_Type;
1777 Item : Ada.Streams.Stream_Element_Array;
1778 Last : out Ada.Streams.Stream_Element_Offset;
1779 To : Sock_Addr_Type;
1780 Flags : Request_Flag_Type := No_Request_Flag)
1782 use type Ada.Streams.Stream_Element_Offset;
1785 Sin : aliased Sockaddr_In;
1786 Len : constant C.int := Sin'Size / 8;
1789 Set_Length (Sin'Unchecked_Access, Len);
1790 Set_Family (Sin'Unchecked_Access, Families (To.Family));
1791 Set_Address (Sin'Unchecked_Access, To_In_Addr (To.Addr));
1793 (Sin'Unchecked_Access,
1794 Short_To_Network (C.unsigned_short (To.Port)));
1798 Item (Item'First)'Address,
1800 Set_Forced_Flags (To_Int (Flags)),
1801 Sin'Unchecked_Access,
1804 if Res = Failure then
1805 Raise_Socket_Error (Socket_Errno);
1808 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1815 procedure Send_Vector
1816 (Socket : Socket_Type;
1817 Vector : Vector_Type;
1818 Count : out Ada.Streams.Stream_Element_Count)
1822 This_Iov_Count : C.int;
1827 while Iov_Count < Vector'Length loop
1829 pragma Warnings (Off);
1830 -- Following test may be compile time known on some targets
1832 if Vector'Length - Iov_Count > Constants.IOV_MAX then
1833 This_Iov_Count := Constants.IOV_MAX;
1835 This_Iov_Count := Vector'Length - Iov_Count;
1838 pragma Warnings (On);
1843 Vector (Vector'First + Integer (Iov_Count))'Address,
1846 if Res = Failure then
1847 Raise_Socket_Error (Socket_Errno);
1850 Count := Count + Ada.Streams.Stream_Element_Count (Res);
1851 Iov_Count := Iov_Count + This_Iov_Count;
1859 procedure Set (Item : in out Socket_Set_Type; Socket : Socket_Type) is
1861 if Item.Set = No_Socket_Set then
1862 Item.Set := New_Socket_Set (No_Socket_Set);
1863 Item.Last := Socket;
1865 elsif Item.Last < Socket then
1866 Item.Last := Socket;
1869 Insert_Socket_In_Set (Item.Set, C.int (Socket));
1872 ----------------------
1873 -- Set_Forced_Flags --
1874 ----------------------
1876 function Set_Forced_Flags (F : C.int) return C.int is
1877 use type C.unsigned;
1878 function To_unsigned is
1879 new Ada.Unchecked_Conversion (C.int, C.unsigned);
1881 new Ada.Unchecked_Conversion (C.unsigned, C.int);
1883 return To_int (To_unsigned (F) or Constants.MSG_Forced_Flags);
1884 end Set_Forced_Flags;
1886 -----------------------
1887 -- Set_Socket_Option --
1888 -----------------------
1890 procedure Set_Socket_Option
1891 (Socket : Socket_Type;
1892 Level : Level_Type := Socket_Level;
1893 Option : Option_Type)
1895 V8 : aliased Two_Int;
1897 V1 : aliased C.unsigned_char;
1898 VT : aliased Timeval;
1900 Add : System.Address := Null_Address;
1909 V4 := C.int (Boolean'Pos (Option.Enabled));
1914 V8 (V8'First) := C.int (Boolean'Pos (Option.Enabled));
1915 V8 (V8'Last) := C.int (Option.Seconds);
1921 V4 := C.int (Option.Size);
1926 V4 := C.int (Boolean'Pos (True));
1930 when Add_Membership |
1932 V8 (V8'First) := To_Int (To_In_Addr (Option.Multicast_Address));
1933 V8 (V8'Last) := To_Int (To_In_Addr (Option.Local_Interface));
1937 when Multicast_If =>
1938 V4 := To_Int (To_In_Addr (Option.Outgoing_If));
1942 when Multicast_TTL =>
1943 V1 := C.unsigned_char (Option.Time_To_Live);
1947 when Multicast_Loop =>
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 Thin.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)));
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 :=
2192 C.Strings.Value (E.S_Name);
2194 Aliases : constant Chars_Ptr_Array :=
2195 Chars_Ptr_Pointers.Value (E.S_Aliases);
2196 -- S_Aliases points to a list of name aliases. The list is
2197 -- terminated by a NULL pointer.
2199 Protocol : constant String :=
2200 C.Strings.Value (E.S_Proto);
2202 Result : Service_Entry_Type
2203 (Aliases_Length => Aliases'Length - 1);
2204 -- The last element is a null pointer
2210 Result.Official := To_Name (Official);
2212 Source := Aliases'First;
2213 Target := Result.Aliases'First;
2214 while Target <= Result.Aliases_Length loop
2215 Result.Aliases (Target) :=
2216 To_Name (C.Strings.Value (Aliases (Source)));
2217 Source := Source + 1;
2218 Target := Target + 1;
2222 Port_Type (Network_To_Short (C.unsigned_short (E.S_Port)));
2224 Result.Protocol := To_Name (Protocol);
2227 end To_Service_Entry;
2233 function To_String (HN : Name_Type) return String is
2235 return HN.Name (1 .. HN.Length);
2242 function To_Timeval (Val : Timeval_Duration) return Timeval is
2247 -- If zero, set result as zero (otherwise it gets rounded down to -1)
2253 -- Normal case where we do round down
2256 S := time_t (Val - 0.5);
2257 uS := suseconds_t (1_000_000 * (Val - Selector_Duration (S)));
2268 (Stream : in out Datagram_Socket_Stream_Type;
2269 Item : Ada.Streams.Stream_Element_Array)
2271 First : Ada.Streams.Stream_Element_Offset := Item'First;
2272 Index : Ada.Streams.Stream_Element_Offset := First - 1;
2273 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
2279 Item (First .. Max),
2283 -- Exit when all or zero data sent. Zero means that the socket has
2284 -- been closed by peer.
2286 exit when Index < First or else Index = Max;
2291 if Index /= Max then
2301 (Stream : in out Stream_Socket_Stream_Type;
2302 Item : Ada.Streams.Stream_Element_Array)
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