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));
360 Set_Address (Sin'Unchecked_Access, To_In_Addr (Address.Addr));
362 (Sin'Unchecked_Access,
363 Short_To_Network (C.unsigned_short (Address.Port)));
365 Res := C_Bind (C.int (Socket), Sin'Address, Len);
367 if Res = Failure then
368 Raise_Socket_Error (Socket_Errno);
376 procedure Check_Selector
377 (Selector : in out Selector_Type;
378 R_Socket_Set : in out Socket_Set_Type;
379 W_Socket_Set : in out Socket_Set_Type;
380 Status : out Selector_Status;
381 Timeout : Selector_Duration := Forever)
383 E_Socket_Set : Socket_Set_Type; -- (No_Socket, No_Socket_Set)
386 (Selector, R_Socket_Set, W_Socket_Set, E_Socket_Set, Status, Timeout);
389 procedure Check_Selector
390 (Selector : in out Selector_Type;
391 R_Socket_Set : in out Socket_Set_Type;
392 W_Socket_Set : in out Socket_Set_Type;
393 E_Socket_Set : in out Socket_Set_Type;
394 Status : out Selector_Status;
395 Timeout : Selector_Duration := Forever)
399 RSig : Socket_Type renames Selector.R_Sig_Socket;
400 RSet : Socket_Set_Type;
401 WSet : Socket_Set_Type;
402 ESet : Socket_Set_Type;
403 TVal : aliased Timeval;
404 TPtr : Timeval_Access;
410 -- No timeout or Forever is indicated by a null timeval pointer
412 if Timeout = Forever then
415 TVal := To_Timeval (Timeout);
416 TPtr := TVal'Unchecked_Access;
419 -- Copy R_Socket_Set in RSet and add read signalling socket
421 RSet := (Set => New_Socket_Set (R_Socket_Set.Set),
422 Last => R_Socket_Set.Last);
425 -- Copy W_Socket_Set in WSet
427 WSet := (Set => New_Socket_Set (W_Socket_Set.Set),
428 Last => W_Socket_Set.Last);
430 -- Copy E_Socket_Set in ESet
432 ESet := (Set => New_Socket_Set (E_Socket_Set.Set),
433 Last => E_Socket_Set.Last);
435 Last := C.int'Max (C.int'Max (C.int (RSet.Last),
447 if Res = Failure then
448 Raise_Socket_Error (Socket_Errno);
451 -- If Select was resumed because of read signalling socket, read this
452 -- data and remove socket from set.
454 if Is_Set (RSet, RSig) then
461 Res := C_Recv (C.int (RSig), Buf'Address, 1, 0);
463 if Res = Failure then
464 Raise_Socket_Error (Socket_Errno);
474 -- Update RSet, WSet and ESet in regard to their new socket sets
480 -- Reset RSet as it should be if R_Sig_Socket was not added
482 if Is_Empty (RSet) then
486 if Is_Empty (WSet) then
490 if Is_Empty (ESet) then
494 -- Deliver RSet, WSet and ESet
496 Empty (R_Socket_Set);
497 R_Socket_Set := RSet;
499 Empty (W_Socket_Set);
500 W_Socket_Set := WSet;
502 Empty (E_Socket_Set);
503 E_Socket_Set := ESet;
509 -- The local socket sets must be emptied before propagating
510 -- Socket_Error so the associated storage is freed.
524 (Item : in out Socket_Set_Type;
525 Socket : Socket_Type)
527 Last : aliased C.int := C.int (Item.Last);
529 if Item.Last /= No_Socket then
530 Remove_Socket_From_Set (Item.Set, C.int (Socket));
531 Last_Socket_In_Set (Item.Set, Last'Unchecked_Access);
532 Item.Last := Socket_Type (Last);
540 procedure Close_Selector (Selector : in out Selector_Type) is
543 -- Close the signalling sockets used internally for the implementation
544 -- of Abort_Selector. Exceptions are ignored because these sockets
545 -- are implementation artefacts of no interest to the user, and
546 -- there is little that can be done if either Close_Socket call fails
547 -- (which theoretically should not happen anyway). We also want to try
548 -- to perform the second Close_Socket even if the first one failed.
551 Close_Socket (Selector.R_Sig_Socket);
558 Close_Socket (Selector.W_Sig_Socket);
569 procedure Close_Socket (Socket : Socket_Type) is
573 Res := C_Close (C.int (Socket));
575 if Res = Failure then
576 Raise_Socket_Error (Socket_Errno);
584 procedure Connect_Socket
585 (Socket : Socket_Type;
586 Server : in out Sock_Addr_Type)
589 Sin : aliased Sockaddr_In;
590 Len : constant C.int := Sin'Size / 8;
593 if Server.Family = Family_Inet6 then
597 Set_Length (Sin'Unchecked_Access, Len);
598 Set_Family (Sin'Unchecked_Access, Families (Server.Family));
599 Set_Address (Sin'Unchecked_Access, To_In_Addr (Server.Addr));
601 (Sin'Unchecked_Access,
602 Short_To_Network (C.unsigned_short (Server.Port)));
604 Res := C_Connect (C.int (Socket), Sin'Address, Len);
606 if Res = Failure then
607 Raise_Socket_Error (Socket_Errno);
615 procedure Control_Socket
616 (Socket : Socket_Type;
617 Request : in out Request_Type)
624 when Non_Blocking_IO =>
625 Arg := C.int (Boolean'Pos (Request.Enabled));
627 when N_Bytes_To_Read =>
634 Requests (Request.Name),
635 Arg'Unchecked_Access);
637 if Res = Failure then
638 Raise_Socket_Error (Socket_Errno);
642 when Non_Blocking_IO =>
645 when N_Bytes_To_Read =>
646 Request.Size := Natural (Arg);
655 (Source : Socket_Set_Type;
656 Target : in out Socket_Set_Type)
660 if Source.Last /= No_Socket then
661 Target.Set := New_Socket_Set (Source.Set);
662 Target.Last := Source.Last;
666 ---------------------
667 -- Create_Selector --
668 ---------------------
670 procedure Create_Selector (Selector : out Selector_Type) is
675 Sin : aliased Sockaddr_In;
676 Len : aliased C.int := Sin'Size / 8;
680 -- We open two signalling sockets. One of them is used to send data to
681 -- the other, which is included in a C_Select socket set. The
682 -- communication is used to force the call to C_Select to complete, and
683 -- the waiting task to resume its execution.
685 -- Create a listening socket
687 S0 := C_Socket (Constants.AF_INET, Constants.SOCK_STREAM, 0);
690 Raise_Socket_Error (Socket_Errno);
693 -- Bind the socket to any unused port on localhost
695 Sin.Sin_Addr.S_B1 := 127;
696 Sin.Sin_Addr.S_B2 := 0;
697 Sin.Sin_Addr.S_B3 := 0;
698 Sin.Sin_Addr.S_B4 := 1;
701 Res := C_Bind (S0, Sin'Address, Len);
703 if Res = Failure then
706 Raise_Socket_Error (Err);
709 -- Get the port used by the socket
711 Res := C_Getsockname (S0, Sin'Address, Len'Access);
713 if Res = Failure then
716 Raise_Socket_Error (Err);
719 -- Set backlog to 1 to guarantee that exactly one call to connect(2)
722 Res := C_Listen (S0, 1);
724 if Res = Failure then
727 Raise_Socket_Error (Err);
730 S1 := C_Socket (Constants.AF_INET, Constants.SOCK_STREAM, 0);
735 Raise_Socket_Error (Err);
738 -- Do a connect and accept the connection
740 Res := C_Connect (S1, Sin'Address, Len);
742 if Res = Failure then
746 Raise_Socket_Error (Err);
749 -- Since the call to connect(2) has suceeded and the backlog limit on
750 -- the listening socket is 1, we know that there is now exactly one
751 -- pending connection on S0, which is the one from S1.
753 S2 := C_Accept (S0, Sin'Address, Len'Access);
759 Raise_Socket_Error (Err);
764 if Res = Failure then
765 Raise_Socket_Error (Socket_Errno);
768 Selector.R_Sig_Socket := Socket_Type (S1);
769 Selector.W_Sig_Socket := Socket_Type (S2);
776 procedure Create_Socket
777 (Socket : out Socket_Type;
778 Family : Family_Type := Family_Inet;
779 Mode : Mode_Type := Socket_Stream)
784 Res := C_Socket (Families (Family), Modes (Mode), 0);
786 if Res = Failure then
787 Raise_Socket_Error (Socket_Errno);
790 Socket := Socket_Type (Res);
797 procedure Empty (Item : in out Socket_Set_Type) is
799 if Item.Set /= No_Socket_Set then
800 Free_Socket_Set (Item.Set);
801 Item.Set := No_Socket_Set;
804 Item.Last := No_Socket;
811 function Err_Code_Image (E : Integer) return String is
812 Msg : String := E'Img & "] ";
814 Msg (Msg'First) := '[';
822 procedure Finalize is
837 (Item : in out Socket_Set_Type;
838 Socket : out Socket_Type)
841 L : aliased C.int := C.int (Item.Last);
844 if Item.Last /= No_Socket then
846 (Item.Set, L'Unchecked_Access, S'Unchecked_Access);
847 Item.Last := Socket_Type (L);
848 Socket := Socket_Type (S);
858 function Get_Address (Stream : Stream_Access) return Sock_Addr_Type is
860 if Stream = null then
862 elsif Stream.all in Datagram_Socket_Stream_Type then
863 return Datagram_Socket_Stream_Type (Stream.all).From;
865 return Get_Peer_Name (Stream_Socket_Stream_Type (Stream.all).Socket);
869 -------------------------
870 -- Get_Host_By_Address --
871 -------------------------
873 function Get_Host_By_Address
874 (Address : Inet_Addr_Type;
875 Family : Family_Type := Family_Inet) return Host_Entry_Type
877 pragma Unreferenced (Family);
879 HA : aliased In_Addr := To_In_Addr (Address);
880 Res : Hostent_Access;
884 -- This C function is not always thread-safe. Protect against
885 -- concurrent access.
888 Res := C_Gethostbyaddr (HA'Address, HA'Size / 8, Constants.AF_INET);
893 Raise_Host_Error (Err);
896 -- Translate from the C format to the API format
899 HE : constant Host_Entry_Type := To_Host_Entry (Res.all);
905 end Get_Host_By_Address;
907 ----------------------
908 -- Get_Host_By_Name --
909 ----------------------
911 function Get_Host_By_Name (Name : String) return Host_Entry_Type is
912 HN : constant C.char_array := C.To_C (Name);
913 Res : Hostent_Access;
917 -- Detect IP address name and redirect to Inet_Addr
919 if Is_IP_Address (Name) then
920 return Get_Host_By_Address (Inet_Addr (Name));
923 -- This C function is not always thread-safe. Protect against
924 -- concurrent access.
927 Res := C_Gethostbyname (HN);
932 Raise_Host_Error (Err);
935 -- Translate from the C format to the API format
938 HE : constant Host_Entry_Type := To_Host_Entry (Res.all);
943 end Get_Host_By_Name;
949 function Get_Peer_Name (Socket : Socket_Type) return Sock_Addr_Type is
950 Sin : aliased Sockaddr_In;
951 Len : aliased C.int := Sin'Size / 8;
952 Res : Sock_Addr_Type (Family_Inet);
955 if C_Getpeername (C.int (Socket), Sin'Address, Len'Access) = Failure then
956 Raise_Socket_Error (Socket_Errno);
959 To_Inet_Addr (Sin.Sin_Addr, Res.Addr);
960 Res.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
965 -------------------------
966 -- Get_Service_By_Name --
967 -------------------------
969 function Get_Service_By_Name
971 Protocol : String) return Service_Entry_Type
973 SN : constant C.char_array := C.To_C (Name);
974 SP : constant C.char_array := C.To_C (Protocol);
975 Res : Servent_Access;
978 -- This C function is not always thread-safe. Protect against
979 -- concurrent access.
982 Res := C_Getservbyname (SN, SP);
986 Ada.Exceptions.Raise_Exception
987 (Service_Error'Identity, "Service not found");
990 -- Translate from the C format to the API format
993 SE : constant Service_Entry_Type := To_Service_Entry (Res.all);
999 end Get_Service_By_Name;
1001 -------------------------
1002 -- Get_Service_By_Port --
1003 -------------------------
1005 function Get_Service_By_Port
1007 Protocol : String) return Service_Entry_Type
1009 SP : constant C.char_array := C.To_C (Protocol);
1010 Res : Servent_Access;
1013 -- This C function is not always thread-safe. Protect against
1014 -- concurrent access.
1017 Res := C_Getservbyport
1018 (C.int (Short_To_Network (C.unsigned_short (Port))), SP);
1022 Ada.Exceptions.Raise_Exception
1023 (Service_Error'Identity, "Service not found");
1026 -- Translate from the C format to the API format
1029 SE : constant Service_Entry_Type := To_Service_Entry (Res.all);
1035 end Get_Service_By_Port;
1037 ---------------------
1038 -- Get_Socket_Name --
1039 ---------------------
1041 function Get_Socket_Name
1042 (Socket : Socket_Type) return Sock_Addr_Type
1044 Sin : aliased Sockaddr_In;
1045 Len : aliased C.int := Sin'Size / 8;
1047 Addr : Sock_Addr_Type := No_Sock_Addr;
1050 Res := C_Getsockname (C.int (Socket), Sin'Address, Len'Access);
1051 if Res /= Failure then
1052 To_Inet_Addr (Sin.Sin_Addr, Addr.Addr);
1053 Addr.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
1057 end Get_Socket_Name;
1059 -----------------------
1060 -- Get_Socket_Option --
1061 -----------------------
1063 function Get_Socket_Option
1064 (Socket : Socket_Type;
1065 Level : Level_Type := Socket_Level;
1066 Name : Option_Name) return Option_Type
1068 use type C.unsigned_char;
1070 V8 : aliased Two_Int;
1072 V1 : aliased C.unsigned_char;
1073 VT : aliased Timeval;
1074 Len : aliased C.int;
1075 Add : System.Address;
1077 Opt : Option_Type (Name);
1081 when Multicast_Loop |
1115 Add, Len'Unchecked_Access);
1117 if Res = Failure then
1118 Raise_Socket_Error (Socket_Errno);
1126 Opt.Enabled := (V4 /= 0);
1129 Opt.Enabled := (V8 (V8'First) /= 0);
1130 Opt.Seconds := Natural (V8 (V8'Last));
1134 Opt.Size := Natural (V4);
1137 Opt.Error := Resolve_Error (Integer (V4));
1139 when Add_Membership |
1141 To_Inet_Addr (To_In_Addr (V8 (V8'First)), Opt.Multicast_Address);
1142 To_Inet_Addr (To_In_Addr (V8 (V8'Last)), Opt.Local_Interface);
1144 when Multicast_If =>
1145 To_Inet_Addr (To_In_Addr (V4), Opt.Outgoing_If);
1147 when Multicast_TTL =>
1148 Opt.Time_To_Live := Integer (V1);
1150 when Multicast_Loop =>
1151 Opt.Enabled := (V1 /= 0);
1155 Opt.Timeout := To_Duration (VT);
1160 end Get_Socket_Option;
1166 function Host_Name return String is
1167 Name : aliased C.char_array (1 .. 64);
1171 Res := C_Gethostname (Name'Address, Name'Length);
1173 if Res = Failure then
1174 Raise_Socket_Error (Socket_Errno);
1177 return C.To_Ada (Name);
1185 (Val : Inet_Addr_VN_Type;
1186 Hex : Boolean := False) return String
1188 -- The largest Inet_Addr_Comp_Type image occurs with IPv4. It
1189 -- has at most a length of 3 plus one '.' character.
1191 Buffer : String (1 .. 4 * Val'Length);
1192 Length : Natural := 1;
1193 Separator : Character;
1195 procedure Img10 (V : Inet_Addr_Comp_Type);
1196 -- Append to Buffer image of V in decimal format
1198 procedure Img16 (V : Inet_Addr_Comp_Type);
1199 -- Append to Buffer image of V in hexadecimal format
1205 procedure Img10 (V : Inet_Addr_Comp_Type) is
1206 Img : constant String := V'Img;
1207 Len : constant Natural := Img'Length - 1;
1209 Buffer (Length .. Length + Len - 1) := Img (2 .. Img'Last);
1210 Length := Length + Len;
1217 procedure Img16 (V : Inet_Addr_Comp_Type) is
1219 Buffer (Length) := Hex_To_Char (Natural (V / 16) + 1);
1220 Buffer (Length + 1) := Hex_To_Char (Natural (V mod 16) + 1);
1221 Length := Length + 2;
1224 -- Start of processing for Image
1233 for J in Val'Range loop
1240 if J /= Val'Last then
1241 Buffer (Length) := Separator;
1242 Length := Length + 1;
1246 return Buffer (1 .. Length - 1);
1253 function Image (Value : Inet_Addr_Type) return String is
1255 if Value.Family = Family_Inet then
1256 return Image (Inet_Addr_VN_Type (Value.Sin_V4), Hex => False);
1258 return Image (Inet_Addr_VN_Type (Value.Sin_V6), Hex => True);
1266 function Image (Value : Sock_Addr_Type) return String is
1267 Port : constant String := Value.Port'Img;
1269 return Image (Value.Addr) & ':' & Port (2 .. Port'Last);
1276 function Image (Socket : Socket_Type) return String is
1285 function Inet_Addr (Image : String) return Inet_Addr_Type is
1286 use Interfaces.C.Strings;
1290 Result : Inet_Addr_Type;
1293 -- Special case for the all-ones broadcast address: this address
1294 -- has the same in_addr_t value as Failure, and thus cannot be
1295 -- properly returned by inet_addr(3).
1297 if Image = "255.255.255.255" then
1298 return Broadcast_Inet_Addr;
1300 -- Special case for an empty Image as on some platforms (e.g. Windows)
1301 -- calling Inet_Addr("") will not return an error.
1303 elsif Image = "" then
1304 Raise_Socket_Error (Constants.EINVAL);
1307 Img := New_String (Image);
1308 Res := C_Inet_Addr (Img);
1311 if Res = Failure then
1312 Raise_Socket_Error (Constants.EINVAL);
1315 To_Inet_Addr (To_In_Addr (Res), Result);
1323 procedure Initialize (Process_Blocking_IO : Boolean := False) is
1325 if not Initialized then
1326 Initialized := True;
1327 Thin.Initialize (Process_Blocking_IO);
1335 function Is_Empty (Item : Socket_Set_Type) return Boolean is
1337 return Item.Last = No_Socket;
1344 function Is_IP_Address (Name : String) return Boolean is
1346 for J in Name'Range loop
1348 and then Name (J) not in '0' .. '9'
1362 (Item : Socket_Set_Type;
1363 Socket : Socket_Type) return Boolean
1366 return Item.Last /= No_Socket
1367 and then Socket <= Item.Last
1368 and then Is_Socket_In_Set (Item.Set, C.int (Socket)) /= 0;
1375 procedure Listen_Socket
1376 (Socket : Socket_Type;
1377 Length : Positive := 15)
1379 Res : constant C.int := C_Listen (C.int (Socket), C.int (Length));
1381 if Res = Failure then
1382 Raise_Socket_Error (Socket_Errno);
1390 procedure Narrow (Item : in out Socket_Set_Type) is
1391 Last : aliased C.int := C.int (Item.Last);
1393 if Item.Set /= No_Socket_Set then
1394 Last_Socket_In_Set (Item.Set, Last'Unchecked_Access);
1395 Item.Last := Socket_Type (Last);
1403 function Official_Name (E : Host_Entry_Type) return String is
1405 return To_String (E.Official);
1412 function Official_Name (S : Service_Entry_Type) return String is
1414 return To_String (S.Official);
1421 function Port_Number (S : Service_Entry_Type) return Port_Type is
1430 function Protocol_Name (S : Service_Entry_Type) return String is
1432 return To_String (S.Protocol);
1435 ----------------------
1436 -- Raise_Host_Error --
1437 ----------------------
1439 procedure Raise_Host_Error (H_Error : Integer) is
1441 function Host_Error_Message return String;
1442 -- We do not use a C function like strerror because hstrerror that would
1443 -- correspond is obsolete. Return appropriate string for error value.
1445 ------------------------
1446 -- Host_Error_Message --
1447 ------------------------
1449 function Host_Error_Message return String is
1452 when Constants.HOST_NOT_FOUND => return "Host not found";
1453 when Constants.TRY_AGAIN => return "Try again";
1454 when Constants.NO_RECOVERY => return "No recovery";
1455 when Constants.NO_DATA => return "No address";
1456 when others => return "Unknown error";
1458 end Host_Error_Message;
1460 -- Start of processing for Raise_Host_Error
1463 Ada.Exceptions.Raise_Exception (Host_Error'Identity,
1464 Err_Code_Image (H_Error)
1465 & Host_Error_Message);
1466 end Raise_Host_Error;
1468 ------------------------
1469 -- Raise_Socket_Error --
1470 ------------------------
1472 procedure Raise_Socket_Error (Error : Integer) is
1473 use type C.Strings.chars_ptr;
1475 Ada.Exceptions.Raise_Exception (Socket_Error'Identity,
1476 Err_Code_Image (Error)
1477 & C.Strings.Value (Socket_Error_Message (Error)));
1478 end Raise_Socket_Error;
1485 (Stream : in out Datagram_Socket_Stream_Type;
1486 Item : out Ada.Streams.Stream_Element_Array;
1487 Last : out Ada.Streams.Stream_Element_Offset)
1489 First : Ada.Streams.Stream_Element_Offset := Item'First;
1490 Index : Ada.Streams.Stream_Element_Offset := First - 1;
1491 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
1497 Item (First .. Max),
1503 -- Exit when all or zero data received. Zero means that the socket
1506 exit when Index < First or else Index = Max;
1517 (Stream : in out Stream_Socket_Stream_Type;
1518 Item : out Ada.Streams.Stream_Element_Array;
1519 Last : out Ada.Streams.Stream_Element_Offset)
1521 First : Ada.Streams.Stream_Element_Offset := Item'First;
1522 Index : Ada.Streams.Stream_Element_Offset := First - 1;
1523 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
1527 Receive_Socket (Stream.Socket, Item (First .. Max), Index);
1530 -- Exit when all or zero data received. Zero means that the socket
1533 exit when Index < First or else Index = Max;
1539 --------------------
1540 -- Receive_Socket --
1541 --------------------
1543 procedure Receive_Socket
1544 (Socket : Socket_Type;
1545 Item : out Ada.Streams.Stream_Element_Array;
1546 Last : out Ada.Streams.Stream_Element_Offset;
1547 Flags : Request_Flag_Type := No_Request_Flag)
1549 use type Ada.Streams.Stream_Element_Offset;
1556 Item (Item'First)'Address,
1560 if Res = Failure then
1561 Raise_Socket_Error (Socket_Errno);
1564 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1567 --------------------
1568 -- Receive_Socket --
1569 --------------------
1571 procedure Receive_Socket
1572 (Socket : Socket_Type;
1573 Item : out Ada.Streams.Stream_Element_Array;
1574 Last : out Ada.Streams.Stream_Element_Offset;
1575 From : out Sock_Addr_Type;
1576 Flags : Request_Flag_Type := No_Request_Flag)
1578 use type Ada.Streams.Stream_Element_Offset;
1581 Sin : aliased Sockaddr_In;
1582 Len : aliased C.int := Sin'Size / 8;
1588 Item (Item'First)'Address,
1591 Sin'Unchecked_Access,
1592 Len'Unchecked_Access);
1594 if Res = Failure then
1595 Raise_Socket_Error (Socket_Errno);
1598 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1600 To_Inet_Addr (Sin.Sin_Addr, From.Addr);
1601 From.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
1608 function Resolve_Error
1609 (Error_Value : Integer;
1610 From_Errno : Boolean := True) return Error_Type
1612 use GNAT.Sockets.Constants;
1615 if not From_Errno then
1617 when Constants.HOST_NOT_FOUND => return Unknown_Host;
1618 when Constants.TRY_AGAIN => return Host_Name_Lookup_Failure;
1619 when Constants.NO_RECOVERY =>
1620 return Non_Recoverable_Error;
1621 when Constants.NO_DATA => return Unknown_Server_Error;
1622 when others => return Cannot_Resolve_Error;
1627 when ENOERROR => return Success;
1628 when EACCES => return Permission_Denied;
1629 when EADDRINUSE => return Address_Already_In_Use;
1630 when EADDRNOTAVAIL => return Cannot_Assign_Requested_Address;
1631 when EAFNOSUPPORT =>
1632 return Address_Family_Not_Supported_By_Protocol;
1633 when EALREADY => return Operation_Already_In_Progress;
1634 when EBADF => return Bad_File_Descriptor;
1635 when ECONNABORTED => return Software_Caused_Connection_Abort;
1636 when ECONNREFUSED => return Connection_Refused;
1637 when ECONNRESET => return Connection_Reset_By_Peer;
1638 when EDESTADDRREQ => return Destination_Address_Required;
1639 when EFAULT => return Bad_Address;
1640 when EHOSTDOWN => return Host_Is_Down;
1641 when EHOSTUNREACH => return No_Route_To_Host;
1642 when EINPROGRESS => return Operation_Now_In_Progress;
1643 when EINTR => return Interrupted_System_Call;
1644 when EINVAL => return Invalid_Argument;
1645 when EIO => return Input_Output_Error;
1646 when EISCONN => return Transport_Endpoint_Already_Connected;
1647 when ELOOP => return Too_Many_Symbolic_Links;
1648 when EMFILE => return Too_Many_Open_Files;
1649 when EMSGSIZE => return Message_Too_Long;
1650 when ENAMETOOLONG => return File_Name_Too_Long;
1651 when ENETDOWN => return Network_Is_Down;
1653 return Network_Dropped_Connection_Because_Of_Reset;
1654 when ENETUNREACH => return Network_Is_Unreachable;
1655 when ENOBUFS => return No_Buffer_Space_Available;
1656 when ENOPROTOOPT => return Protocol_Not_Available;
1657 when ENOTCONN => return Transport_Endpoint_Not_Connected;
1658 when ENOTSOCK => return Socket_Operation_On_Non_Socket;
1659 when EOPNOTSUPP => return Operation_Not_Supported;
1660 when EPFNOSUPPORT => return Protocol_Family_Not_Supported;
1661 when EPROTONOSUPPORT => return Protocol_Not_Supported;
1662 when EPROTOTYPE => return Protocol_Wrong_Type_For_Socket;
1664 return Cannot_Send_After_Transport_Endpoint_Shutdown;
1665 when ESOCKTNOSUPPORT => return Socket_Type_Not_Supported;
1666 when ETIMEDOUT => return Connection_Timed_Out;
1667 when ETOOMANYREFS => return Too_Many_References;
1668 when EWOULDBLOCK => return Resource_Temporarily_Unavailable;
1669 when others => null;
1672 return Cannot_Resolve_Error;
1675 -----------------------
1676 -- Resolve_Exception --
1677 -----------------------
1679 function Resolve_Exception
1680 (Occurrence : Exception_Occurrence) return Error_Type
1682 Id : constant Exception_Id := Exception_Identity (Occurrence);
1683 Msg : constant String := Exception_Message (Occurrence);
1690 while First <= Msg'Last
1691 and then Msg (First) not in '0' .. '9'
1696 if First > Msg'Last then
1697 return Cannot_Resolve_Error;
1701 while Last < Msg'Last
1702 and then Msg (Last + 1) in '0' .. '9'
1707 Val := Integer'Value (Msg (First .. Last));
1709 if Id = Socket_Error_Id then
1710 return Resolve_Error (Val);
1711 elsif Id = Host_Error_Id then
1712 return Resolve_Error (Val, False);
1714 return Cannot_Resolve_Error;
1716 end Resolve_Exception;
1718 --------------------
1719 -- Receive_Vector --
1720 --------------------
1722 procedure Receive_Vector
1723 (Socket : Socket_Type;
1724 Vector : Vector_Type;
1725 Count : out Ada.Streams.Stream_Element_Count)
1733 Vector (Vector'First)'Address,
1736 if Res = Failure then
1737 Raise_Socket_Error (Socket_Errno);
1740 Count := Ada.Streams.Stream_Element_Count (Res);
1747 procedure Send_Socket
1748 (Socket : Socket_Type;
1749 Item : Ada.Streams.Stream_Element_Array;
1750 Last : out Ada.Streams.Stream_Element_Offset;
1751 Flags : Request_Flag_Type := No_Request_Flag)
1753 use type Ada.Streams.Stream_Element_Offset;
1761 Item (Item'First)'Address,
1763 Set_Forced_Flags (To_Int (Flags)));
1765 if Res = Failure then
1766 Raise_Socket_Error (Socket_Errno);
1769 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1776 procedure Send_Socket
1777 (Socket : Socket_Type;
1778 Item : Ada.Streams.Stream_Element_Array;
1779 Last : out Ada.Streams.Stream_Element_Offset;
1780 To : Sock_Addr_Type;
1781 Flags : Request_Flag_Type := No_Request_Flag)
1783 use type Ada.Streams.Stream_Element_Offset;
1786 Sin : aliased Sockaddr_In;
1787 Len : constant C.int := Sin'Size / 8;
1790 Set_Length (Sin'Unchecked_Access, Len);
1791 Set_Family (Sin'Unchecked_Access, Families (To.Family));
1792 Set_Address (Sin'Unchecked_Access, To_In_Addr (To.Addr));
1794 (Sin'Unchecked_Access,
1795 Short_To_Network (C.unsigned_short (To.Port)));
1799 Item (Item'First)'Address,
1801 Set_Forced_Flags (To_Int (Flags)),
1802 Sin'Unchecked_Access,
1805 if Res = Failure then
1806 Raise_Socket_Error (Socket_Errno);
1809 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1816 procedure Send_Vector
1817 (Socket : Socket_Type;
1818 Vector : Vector_Type;
1819 Count : out Ada.Streams.Stream_Element_Count)
1823 This_Iov_Count : C.int;
1828 while Iov_Count < Vector'Length loop
1830 pragma Warnings (Off);
1831 -- Following test may be compile time known on some targets
1833 if Vector'Length - Iov_Count > Constants.IOV_MAX then
1834 This_Iov_Count := Constants.IOV_MAX;
1836 This_Iov_Count := Vector'Length - Iov_Count;
1839 pragma Warnings (On);
1844 Vector (Vector'First + Integer (Iov_Count))'Address,
1847 if Res = Failure then
1848 Raise_Socket_Error (Socket_Errno);
1851 Count := Count + Ada.Streams.Stream_Element_Count (Res);
1852 Iov_Count := Iov_Count + This_Iov_Count;
1860 procedure Set (Item : in out Socket_Set_Type; Socket : Socket_Type) is
1862 if Item.Set = No_Socket_Set then
1863 Item.Set := New_Socket_Set (No_Socket_Set);
1864 Item.Last := Socket;
1866 elsif Item.Last < Socket then
1867 Item.Last := Socket;
1870 Insert_Socket_In_Set (Item.Set, C.int (Socket));
1873 ----------------------
1874 -- Set_Forced_Flags --
1875 ----------------------
1877 function Set_Forced_Flags (F : C.int) return C.int is
1878 use type C.unsigned;
1879 function To_unsigned is
1880 new Ada.Unchecked_Conversion (C.int, C.unsigned);
1882 new Ada.Unchecked_Conversion (C.unsigned, C.int);
1884 return To_int (To_unsigned (F) or Constants.MSG_Forced_Flags);
1885 end Set_Forced_Flags;
1887 -----------------------
1888 -- Set_Socket_Option --
1889 -----------------------
1891 procedure Set_Socket_Option
1892 (Socket : Socket_Type;
1893 Level : Level_Type := Socket_Level;
1894 Option : Option_Type)
1896 V8 : aliased Two_Int;
1898 V1 : aliased C.unsigned_char;
1899 VT : aliased Timeval;
1901 Add : System.Address := Null_Address;
1910 V4 := C.int (Boolean'Pos (Option.Enabled));
1915 V8 (V8'First) := C.int (Boolean'Pos (Option.Enabled));
1916 V8 (V8'Last) := C.int (Option.Seconds);
1922 V4 := C.int (Option.Size);
1927 V4 := C.int (Boolean'Pos (True));
1931 when Add_Membership |
1933 V8 (V8'First) := To_Int (To_In_Addr (Option.Multicast_Address));
1934 V8 (V8'Last) := To_Int (To_In_Addr (Option.Local_Interface));
1938 when Multicast_If =>
1939 V4 := To_Int (To_In_Addr (Option.Outgoing_If));
1943 when Multicast_TTL =>
1944 V1 := C.unsigned_char (Option.Time_To_Live);
1948 when Multicast_Loop =>
1949 V1 := C.unsigned_char (Boolean'Pos (Option.Enabled));
1955 VT := To_Timeval (Option.Timeout);
1964 Options (Option.Name),
1967 if Res = Failure then
1968 Raise_Socket_Error (Socket_Errno);
1970 end Set_Socket_Option;
1972 ----------------------
1973 -- Short_To_Network --
1974 ----------------------
1976 function Short_To_Network (S : C.unsigned_short) return C.unsigned_short is
1977 use type C.unsigned_short;
1980 -- Big-endian case. No conversion needed. On these platforms,
1981 -- htons() defaults to a null procedure.
1983 pragma Warnings (Off);
1984 -- Since the test can generate "always True/False" warning
1986 if Default_Bit_Order = High_Order_First then
1989 pragma Warnings (On);
1991 -- Little-endian case. We must swap the high and low bytes of this
1992 -- short to make the port number network compliant.
1995 return (S / 256) + (S mod 256) * 256;
1997 end Short_To_Network;
1999 ---------------------
2000 -- Shutdown_Socket --
2001 ---------------------
2003 procedure Shutdown_Socket
2004 (Socket : Socket_Type;
2005 How : Shutmode_Type := Shut_Read_Write)
2010 Res := C_Shutdown (C.int (Socket), Shutmodes (How));
2012 if Res = Failure then
2013 Raise_Socket_Error (Socket_Errno);
2015 end Shutdown_Socket;
2022 (Socket : Socket_Type;
2023 Send_To : Sock_Addr_Type) return Stream_Access
2025 S : Datagram_Socket_Stream_Access;
2028 S := new Datagram_Socket_Stream_Type;
2031 S.From := Get_Socket_Name (Socket);
2032 return Stream_Access (S);
2039 function Stream (Socket : Socket_Type) return Stream_Access is
2040 S : Stream_Socket_Stream_Access;
2042 S := new Stream_Socket_Stream_Type;
2044 return Stream_Access (S);
2051 function To_C (Socket : Socket_Type) return Integer is
2053 return Integer (Socket);
2060 function To_Duration (Val : Timeval) return Timeval_Duration is
2062 return Natural (Val.Tv_Sec) * 1.0 + Natural (Val.Tv_Usec) * 1.0E-6;
2069 function To_Host_Entry (E : Hostent) return Host_Entry_Type is
2072 Official : constant String :=
2073 C.Strings.Value (E.H_Name);
2075 Aliases : constant Chars_Ptr_Array :=
2076 Chars_Ptr_Pointers.Value (E.H_Aliases);
2077 -- H_Aliases points to a list of name aliases. The list is terminated by
2080 Addresses : constant In_Addr_Access_Array :=
2081 In_Addr_Access_Pointers.Value (E.H_Addr_List);
2082 -- H_Addr_List points to a list of binary addresses (in network byte
2083 -- order). The list is terminated by a NULL pointer.
2085 -- H_Length is not used because it is currently only set to 4.
2086 -- H_Addrtype is always AF_INET
2088 Result : Host_Entry_Type
2089 (Aliases_Length => Aliases'Length - 1,
2090 Addresses_Length => Addresses'Length - 1);
2091 -- The last element is a null pointer
2097 Result.Official := To_Name (Official);
2099 Source := Aliases'First;
2100 Target := Result.Aliases'First;
2101 while Target <= Result.Aliases_Length loop
2102 Result.Aliases (Target) :=
2103 To_Name (C.Strings.Value (Aliases (Source)));
2104 Source := Source + 1;
2105 Target := Target + 1;
2108 Source := Addresses'First;
2109 Target := Result.Addresses'First;
2110 while Target <= Result.Addresses_Length loop
2111 To_Inet_Addr (Addresses (Source).all, Result.Addresses (Target));
2112 Source := Source + 1;
2113 Target := Target + 1;
2123 function To_In_Addr (Addr : Inet_Addr_Type) return Thin.In_Addr is
2125 if Addr.Family = Family_Inet then
2126 return (S_B1 => C.unsigned_char (Addr.Sin_V4 (1)),
2127 S_B2 => C.unsigned_char (Addr.Sin_V4 (2)),
2128 S_B3 => C.unsigned_char (Addr.Sin_V4 (3)),
2129 S_B4 => C.unsigned_char (Addr.Sin_V4 (4)));
2139 procedure To_Inet_Addr
2141 Result : out Inet_Addr_Type) is
2143 Result.Sin_V4 (1) := Inet_Addr_Comp_Type (Addr.S_B1);
2144 Result.Sin_V4 (2) := Inet_Addr_Comp_Type (Addr.S_B2);
2145 Result.Sin_V4 (3) := Inet_Addr_Comp_Type (Addr.S_B3);
2146 Result.Sin_V4 (4) := Inet_Addr_Comp_Type (Addr.S_B4);
2153 function To_Int (F : Request_Flag_Type) return C.int
2155 Current : Request_Flag_Type := F;
2156 Result : C.int := 0;
2159 for J in Flags'Range loop
2160 exit when Current = 0;
2162 if Current mod 2 /= 0 then
2163 if Flags (J) = -1 then
2164 Raise_Socket_Error (Constants.EOPNOTSUPP);
2167 Result := Result + Flags (J);
2170 Current := Current / 2;
2180 function To_Name (N : String) return Name_Type is
2182 return Name_Type'(N'Length, N);
2185 ----------------------
2186 -- To_Service_Entry --
2187 ----------------------
2189 function To_Service_Entry (E : Servent) return Service_Entry_Type is
2192 Official : constant String :=
2193 C.Strings.Value (E.S_Name);
2195 Aliases : constant Chars_Ptr_Array :=
2196 Chars_Ptr_Pointers.Value (E.S_Aliases);
2197 -- S_Aliases points to a list of name aliases. The list is
2198 -- terminated by a NULL pointer.
2200 Protocol : constant String :=
2201 C.Strings.Value (E.S_Proto);
2203 Result : Service_Entry_Type
2204 (Aliases_Length => Aliases'Length - 1);
2205 -- The last element is a null pointer
2211 Result.Official := To_Name (Official);
2213 Source := Aliases'First;
2214 Target := Result.Aliases'First;
2215 while Target <= Result.Aliases_Length loop
2216 Result.Aliases (Target) :=
2217 To_Name (C.Strings.Value (Aliases (Source)));
2218 Source := Source + 1;
2219 Target := Target + 1;
2223 Port_Type (Network_To_Short (C.unsigned_short (E.S_Port)));
2225 Result.Protocol := To_Name (Protocol);
2228 end To_Service_Entry;
2234 function To_String (HN : Name_Type) return String is
2236 return HN.Name (1 .. HN.Length);
2243 function To_Timeval (Val : Timeval_Duration) return Timeval is
2248 -- If zero, set result as zero (otherwise it gets rounded down to -1)
2254 -- Normal case where we do round down
2257 S := time_t (Val - 0.5);
2258 uS := suseconds_t (1_000_000 * (Val - Selector_Duration (S)));
2269 (Stream : in out Datagram_Socket_Stream_Type;
2270 Item : Ada.Streams.Stream_Element_Array)
2272 First : Ada.Streams.Stream_Element_Offset := Item'First;
2273 Index : Ada.Streams.Stream_Element_Offset := First - 1;
2274 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
2280 Item (First .. Max),
2284 -- Exit when all or zero data sent. Zero means that the socket has
2285 -- been closed by peer.
2287 exit when Index < First or else Index = Max;
2292 if Index /= Max then
2302 (Stream : in out Stream_Socket_Stream_Type;
2303 Item : Ada.Streams.Stream_Element_Array)
2305 First : Ada.Streams.Stream_Element_Offset := Item'First;
2306 Index : Ada.Streams.Stream_Element_Offset := First - 1;
2307 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
2311 Send_Socket (Stream.Socket, Item (First .. Max), Index);
2313 -- Exit when all or zero data sent. Zero means that the socket has
2314 -- been closed by peer.
2316 exit when Index < First or else Index = Max;
2321 if Index /= Max then