1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S --
9 -- Copyright (C) 2001-2008, AdaCore --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with Ada.Streams; use Ada.Streams;
35 with Ada.Exceptions; use Ada.Exceptions;
36 with Ada.Unchecked_Conversion;
38 with Interfaces.C.Strings;
40 with GNAT.Sockets.Constants;
41 with GNAT.Sockets.Thin_Common; use GNAT.Sockets.Thin_Common;
42 with GNAT.Sockets.Thin; use GNAT.Sockets.Thin;
43 with GNAT.Sockets.Thin.Task_Safe_NetDB; use GNAT.Sockets.Thin.Task_Safe_NetDB;
45 with GNAT.Sockets.Linker_Options;
46 pragma Warnings (Off, GNAT.Sockets.Linker_Options);
47 -- Need to include pragma Linker_Options which is platform dependent
49 with System; use System;
51 package body GNAT.Sockets is
53 package C renames Interfaces.C;
57 Finalized : Boolean := False;
58 Initialized : Boolean := False;
60 ENOERROR : constant := 0;
62 Netdb_Buffer_Size : constant := Constants.Need_Netdb_Buffer * 1024;
63 -- The network database functions gethostbyname, gethostbyaddr,
64 -- getservbyname and getservbyport can either be guaranteed task safe by
65 -- the operating system, or else return data through a user-provided buffer
66 -- to ensure concurrent uses do not interfere.
68 -- Correspondence tables
70 Levels : constant array (Level_Type) of C.int :=
71 (Socket_Level => Constants.SOL_SOCKET,
72 IP_Protocol_For_IP_Level => Constants.IPPROTO_IP,
73 IP_Protocol_For_UDP_Level => Constants.IPPROTO_UDP,
74 IP_Protocol_For_TCP_Level => Constants.IPPROTO_TCP);
76 Modes : constant array (Mode_Type) of C.int :=
77 (Socket_Stream => Constants.SOCK_STREAM,
78 Socket_Datagram => Constants.SOCK_DGRAM);
80 Shutmodes : constant array (Shutmode_Type) of C.int :=
81 (Shut_Read => Constants.SHUT_RD,
82 Shut_Write => Constants.SHUT_WR,
83 Shut_Read_Write => Constants.SHUT_RDWR);
85 Requests : constant array (Request_Name) of C.int :=
86 (Non_Blocking_IO => Constants.FIONBIO,
87 N_Bytes_To_Read => Constants.FIONREAD);
89 Options : constant array (Option_Name) of C.int :=
90 (Keep_Alive => Constants.SO_KEEPALIVE,
91 Reuse_Address => Constants.SO_REUSEADDR,
92 Broadcast => Constants.SO_BROADCAST,
93 Send_Buffer => Constants.SO_SNDBUF,
94 Receive_Buffer => Constants.SO_RCVBUF,
95 Linger => Constants.SO_LINGER,
96 Error => Constants.SO_ERROR,
97 No_Delay => Constants.TCP_NODELAY,
98 Add_Membership => Constants.IP_ADD_MEMBERSHIP,
99 Drop_Membership => Constants.IP_DROP_MEMBERSHIP,
100 Multicast_If => Constants.IP_MULTICAST_IF,
101 Multicast_TTL => Constants.IP_MULTICAST_TTL,
102 Multicast_Loop => Constants.IP_MULTICAST_LOOP,
103 Receive_Packet_Info => Constants.IP_PKTINFO,
104 Send_Timeout => Constants.SO_SNDTIMEO,
105 Receive_Timeout => Constants.SO_RCVTIMEO);
106 -- ??? Note: for OpenSolaris, Receive_Packet_Info should be IP_RECVPKTINFO,
107 -- but for Linux compatibility this constant is the same as IP_PKTINFO.
109 Flags : constant array (0 .. 3) of C.int :=
110 (0 => Constants.MSG_OOB, -- Process_Out_Of_Band_Data
111 1 => Constants.MSG_PEEK, -- Peek_At_Incoming_Data
112 2 => Constants.MSG_WAITALL, -- Wait_For_A_Full_Reception
113 3 => Constants.MSG_EOR); -- Send_End_Of_Record
115 Socket_Error_Id : constant Exception_Id := Socket_Error'Identity;
116 Host_Error_Id : constant Exception_Id := Host_Error'Identity;
118 Hex_To_Char : constant String (1 .. 16) := "0123456789ABCDEF";
119 -- Use to print in hexadecimal format
121 function Err_Code_Image (E : Integer) return String;
122 -- Return the value of E surrounded with brackets
124 -----------------------
125 -- Local subprograms --
126 -----------------------
128 function Resolve_Error
129 (Error_Value : Integer;
130 From_Errno : Boolean := True) return Error_Type;
131 -- Associate an enumeration value (error_type) to en error value (errno).
132 -- From_Errno prevents from mixing h_errno with errno.
134 function To_Name (N : String) return Name_Type;
135 function To_String (HN : Name_Type) return String;
136 -- Conversion functions
138 function To_Int (F : Request_Flag_Type) return C.int;
139 -- Return the int value corresponding to the specified flags combination
141 function Set_Forced_Flags (F : C.int) return C.int;
142 -- Return F with the bits from Constants.MSG_Forced_Flags forced set
144 function Short_To_Network
145 (S : C.unsigned_short) return C.unsigned_short;
146 pragma Inline (Short_To_Network);
147 -- Convert a port number into a network port number
149 function Network_To_Short
150 (S : C.unsigned_short) return C.unsigned_short
151 renames Short_To_Network;
152 -- Symmetric operation
155 (Val : Inet_Addr_VN_Type;
156 Hex : Boolean := False) return String;
157 -- Output an array of inet address components in hex or decimal mode
159 function Is_IP_Address (Name : String) return Boolean;
160 -- Return true when Name is an IP address in standard dot notation
162 function To_In_Addr (Addr : Inet_Addr_Type) return In_Addr;
163 procedure To_Inet_Addr
165 Result : out Inet_Addr_Type);
166 -- Conversion functions
168 function To_Host_Entry (E : Hostent) return Host_Entry_Type;
169 -- Conversion function
171 function To_Service_Entry (E : Servent) return Service_Entry_Type;
172 -- Conversion function
174 function To_Timeval (Val : Timeval_Duration) return Timeval;
175 -- Separate Val in seconds and microseconds
177 function To_Duration (Val : Timeval) return Timeval_Duration;
178 -- Reconstruct a Duration value from a Timeval record (seconds and
181 procedure Raise_Socket_Error (Error : Integer);
182 -- Raise Socket_Error with an exception message describing the error code
185 procedure Raise_Host_Error (H_Error : Integer);
186 -- Raise Host_Error exception with message describing error code (note
187 -- hstrerror seems to be obsolete) from h_errno.
189 procedure Narrow (Item : in out Socket_Set_Type);
190 -- Update Last as it may be greater than the real last socket
192 -- Types needed for Datagram_Socket_Stream_Type
194 type Datagram_Socket_Stream_Type is new Root_Stream_Type with record
195 Socket : Socket_Type;
197 From : Sock_Addr_Type;
200 type Datagram_Socket_Stream_Access is
201 access all Datagram_Socket_Stream_Type;
204 (Stream : in out Datagram_Socket_Stream_Type;
205 Item : out Ada.Streams.Stream_Element_Array;
206 Last : out Ada.Streams.Stream_Element_Offset);
209 (Stream : in out Datagram_Socket_Stream_Type;
210 Item : Ada.Streams.Stream_Element_Array);
212 -- Types needed for Stream_Socket_Stream_Type
214 type Stream_Socket_Stream_Type is new Root_Stream_Type with record
215 Socket : Socket_Type;
218 type Stream_Socket_Stream_Access is
219 access all Stream_Socket_Stream_Type;
222 (Stream : in out Stream_Socket_Stream_Type;
223 Item : out Ada.Streams.Stream_Element_Array;
224 Last : out Ada.Streams.Stream_Element_Offset);
227 (Stream : in out Stream_Socket_Stream_Type;
228 Item : Ada.Streams.Stream_Element_Array);
230 procedure Wait_On_Socket
231 (Socket : Socket_Type;
233 Timeout : Selector_Duration;
234 Selector : access Selector_Type := null;
235 Status : out Selector_Status);
236 -- Common code for variants of socket operations supporting a timeout:
237 -- block in Check_Selector on Socket for at most the indicated timeout.
238 -- If For_Read is True, Socket is added to the read set for this call, else
239 -- it is added to the write set. If no selector is provided, a local one is
240 -- created for this call and destroyed prior to returning.
246 function "+" (L, R : Request_Flag_Type) return Request_Flag_Type is
255 procedure Abort_Selector (Selector : Selector_Type) is
259 -- Send one byte to unblock select system call
261 Res := Signalling_Fds.Write (C.int (Selector.W_Sig_Socket));
263 if Res = Failure then
264 Raise_Socket_Error (Socket_Errno);
272 procedure Accept_Socket
273 (Server : Socket_Type;
274 Socket : out Socket_Type;
275 Address : out Sock_Addr_Type)
278 Sin : aliased Sockaddr_In;
279 Len : aliased C.int := Sin'Size / 8;
282 Res := C_Accept (C.int (Server), Sin'Address, Len'Access);
284 if Res = Failure then
285 Raise_Socket_Error (Socket_Errno);
288 Socket := Socket_Type (Res);
290 To_Inet_Addr (Sin.Sin_Addr, Address.Addr);
291 Address.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
298 procedure Accept_Socket
299 (Server : Socket_Type;
300 Socket : out Socket_Type;
301 Address : out Sock_Addr_Type;
302 Timeout : Selector_Duration;
303 Selector : access Selector_Type := null;
304 Status : out Selector_Status)
307 -- Wait for socket to become available for reading
313 Selector => Selector,
316 -- Accept connection if available
318 if Status = Completed then
319 Accept_Socket (Server, Socket, Address);
330 (E : Host_Entry_Type;
331 N : Positive := 1) return Inet_Addr_Type
334 return E.Addresses (N);
337 ----------------------
338 -- Addresses_Length --
339 ----------------------
341 function Addresses_Length (E : Host_Entry_Type) return Natural is
343 return E.Addresses_Length;
344 end Addresses_Length;
351 (E : Host_Entry_Type;
352 N : Positive := 1) return String
355 return To_String (E.Aliases (N));
363 (S : Service_Entry_Type;
364 N : Positive := 1) return String
367 return To_String (S.Aliases (N));
374 function Aliases_Length (E : Host_Entry_Type) return Natural is
376 return E.Aliases_Length;
383 function Aliases_Length (S : Service_Entry_Type) return Natural is
385 return S.Aliases_Length;
392 procedure Bind_Socket
393 (Socket : Socket_Type;
394 Address : Sock_Addr_Type)
397 Sin : aliased Sockaddr_In;
398 Len : constant C.int := Sin'Size / 8;
399 -- This assumes that Address.Family = Family_Inet???
402 if Address.Family = Family_Inet6 then
403 raise Socket_Error with "IPv6 not supported";
406 Set_Family (Sin.Sin_Family, Address.Family);
407 Set_Address (Sin'Unchecked_Access, To_In_Addr (Address.Addr));
409 (Sin'Unchecked_Access,
410 Short_To_Network (C.unsigned_short (Address.Port)));
412 Res := C_Bind (C.int (Socket), Sin'Address, Len);
414 if Res = Failure then
415 Raise_Socket_Error (Socket_Errno);
423 procedure Check_Selector
424 (Selector : in out Selector_Type;
425 R_Socket_Set : in out Socket_Set_Type;
426 W_Socket_Set : in out Socket_Set_Type;
427 Status : out Selector_Status;
428 Timeout : Selector_Duration := Forever)
430 E_Socket_Set : Socket_Set_Type; -- (No_Socket, No_Fd_Set_Access)
433 (Selector, R_Socket_Set, W_Socket_Set, E_Socket_Set, Status, Timeout);
440 procedure Check_Selector
441 (Selector : in out Selector_Type;
442 R_Socket_Set : in out Socket_Set_Type;
443 W_Socket_Set : in out Socket_Set_Type;
444 E_Socket_Set : in out Socket_Set_Type;
445 Status : out Selector_Status;
446 Timeout : Selector_Duration := Forever)
450 RSig : Socket_Type renames Selector.R_Sig_Socket;
451 RSet : Socket_Set_Type;
452 WSet : Socket_Set_Type;
453 ESet : Socket_Set_Type;
454 TVal : aliased Timeval;
455 TPtr : Timeval_Access;
461 -- No timeout or Forever is indicated by a null timeval pointer
463 if Timeout = Forever then
466 TVal := To_Timeval (Timeout);
467 TPtr := TVal'Unchecked_Access;
470 -- Copy R_Socket_Set in RSet and add read signalling socket
472 RSet := (Set => New_Socket_Set (R_Socket_Set.Set),
473 Last => R_Socket_Set.Last);
476 -- Copy W_Socket_Set in WSet
478 WSet := (Set => New_Socket_Set (W_Socket_Set.Set),
479 Last => W_Socket_Set.Last);
481 -- Copy E_Socket_Set in ESet
483 ESet := (Set => New_Socket_Set (E_Socket_Set.Set),
484 Last => E_Socket_Set.Last);
486 Last := C.int'Max (C.int'Max (C.int (RSet.Last),
498 if Res = Failure then
499 Raise_Socket_Error (Socket_Errno);
502 -- If Select was resumed because of read signalling socket, read this
503 -- data and remove socket from set.
505 if Is_Set (RSet, RSig) then
508 Res := Signalling_Fds.Read (C.int (RSig));
510 if Res = Failure then
511 Raise_Socket_Error (Socket_Errno);
520 -- Update RSet, WSet and ESet in regard to their new socket sets
526 -- Reset RSet as it should be if R_Sig_Socket was not added
528 if Is_Empty (RSet) then
532 if Is_Empty (WSet) then
536 if Is_Empty (ESet) then
540 -- Deliver RSet, WSet and ESet
542 Empty (R_Socket_Set);
543 R_Socket_Set := RSet;
545 Empty (W_Socket_Set);
546 W_Socket_Set := WSet;
548 Empty (E_Socket_Set);
549 E_Socket_Set := ESet;
554 -- The local socket sets must be emptied before propagating
555 -- Socket_Error so the associated storage is freed.
569 (Item : in out Socket_Set_Type;
570 Socket : Socket_Type)
572 Last : aliased C.int := C.int (Item.Last);
574 if Item.Last /= No_Socket then
575 Remove_Socket_From_Set (Item.Set, C.int (Socket));
576 Last_Socket_In_Set (Item.Set, Last'Unchecked_Access);
577 Item.Last := Socket_Type (Last);
585 procedure Close_Selector (Selector : in out Selector_Type) is
587 -- Close the signalling file descriptors used internally for the
588 -- implementation of Abort_Selector.
590 Signalling_Fds.Close (C.int (Selector.R_Sig_Socket));
591 Signalling_Fds.Close (C.int (Selector.W_Sig_Socket));
593 -- Reset R_Sig_Socket and W_Sig_Socket to No_Socket to ensure that any
594 -- (erroneous) subsequent attempt to use this selector properly fails.
596 Selector.R_Sig_Socket := No_Socket;
597 Selector.W_Sig_Socket := No_Socket;
604 procedure Close_Socket (Socket : Socket_Type) is
608 Res := C_Close (C.int (Socket));
610 if Res = Failure then
611 Raise_Socket_Error (Socket_Errno);
619 procedure Connect_Socket
620 (Socket : Socket_Type;
621 Server : Sock_Addr_Type)
624 Sin : aliased Sockaddr_In;
625 Len : constant C.int := Sin'Size / 8;
628 if Server.Family = Family_Inet6 then
629 raise Socket_Error with "IPv6 not supported";
632 Set_Family (Sin.Sin_Family, Server.Family);
633 Set_Address (Sin'Unchecked_Access, To_In_Addr (Server.Addr));
635 (Sin'Unchecked_Access,
636 Short_To_Network (C.unsigned_short (Server.Port)));
638 Res := C_Connect (C.int (Socket), Sin'Address, Len);
640 if Res = Failure then
641 Raise_Socket_Error (Socket_Errno);
649 procedure Connect_Socket
650 (Socket : Socket_Type;
651 Server : Sock_Addr_Type;
652 Timeout : Selector_Duration;
653 Selector : access Selector_Type := null;
654 Status : out Selector_Status)
657 -- Used to set Socket to non-blocking I/O
660 -- Set the socket to non-blocking I/O
662 Req := (Name => Non_Blocking_IO, Enabled => True);
663 Control_Socket (Socket, Request => Req);
665 -- Start operation (non-blocking), will raise Socket_Error with
669 Connect_Socket (Socket, Server);
671 when E : Socket_Error =>
672 if Resolve_Exception (E) = Operation_Now_In_Progress then
679 -- Wait for socket to become available for writing
685 Selector => Selector,
688 -- Reset the socket to blocking I/O
690 Req := (Name => Non_Blocking_IO, Enabled => False);
691 Control_Socket (Socket, Request => Req);
698 procedure Control_Socket
699 (Socket : Socket_Type;
700 Request : in out Request_Type)
707 when Non_Blocking_IO =>
708 Arg := C.int (Boolean'Pos (Request.Enabled));
710 when N_Bytes_To_Read =>
716 Requests (Request.Name),
717 Arg'Unchecked_Access);
719 if Res = Failure then
720 Raise_Socket_Error (Socket_Errno);
724 when Non_Blocking_IO =>
727 when N_Bytes_To_Read =>
728 Request.Size := Natural (Arg);
737 (Source : Socket_Set_Type;
738 Target : in out Socket_Set_Type)
742 if Source.Last /= No_Socket then
743 Target.Set := New_Socket_Set (Source.Set);
744 Target.Last := Source.Last;
748 ---------------------
749 -- Create_Selector --
750 ---------------------
752 procedure Create_Selector (Selector : out Selector_Type) is
753 Two_Fds : aliased Fd_Pair;
757 -- We open two signalling file descriptors. One of them is used to send
758 -- data to the other, which is included in a C_Select socket set. The
759 -- communication is used to force a call to C_Select to complete, and
760 -- the waiting task to resume its execution.
762 Res := Signalling_Fds.Create (Two_Fds'Access);
764 if Res = Failure then
765 Raise_Socket_Error (Socket_Errno);
768 Selector.R_Sig_Socket := Socket_Type (Two_Fds (Read_End));
769 Selector.W_Sig_Socket := Socket_Type (Two_Fds (Write_End));
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_Fd_Set_Access then
800 Free_Socket_Set (Item.Set);
801 Item.Set := No_Fd_Set_Access;
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);
859 (Stream : not null Stream_Access) return Sock_Addr_Type
862 if Stream.all in Datagram_Socket_Stream_Type then
863 return Datagram_Socket_Stream_Type (Stream.all).From;
865 return Get_Peer_Name (Stream_Socket_Stream_Type (Stream.all).Socket);
869 -------------------------
870 -- Get_Host_By_Address --
871 -------------------------
873 function Get_Host_By_Address
874 (Address : Inet_Addr_Type;
875 Family : Family_Type := Family_Inet) return Host_Entry_Type
877 pragma Unreferenced (Family);
879 HA : aliased In_Addr := To_In_Addr (Address);
880 Buflen : constant C.int := Netdb_Buffer_Size;
881 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
882 Res : aliased Hostent;
886 if Safe_Gethostbyaddr (HA'Address, HA'Size / 8, Constants.AF_INET,
887 Res'Access, Buf'Address, Buflen, Err'Access) /= 0
889 Raise_Host_Error (Integer (Err));
892 return To_Host_Entry (Res);
893 end Get_Host_By_Address;
895 ----------------------
896 -- Get_Host_By_Name --
897 ----------------------
899 function Get_Host_By_Name (Name : String) return Host_Entry_Type is
901 -- Detect IP address name and redirect to Inet_Addr
903 if Is_IP_Address (Name) then
904 return Get_Host_By_Address (Inet_Addr (Name));
908 HN : constant C.char_array := C.To_C (Name);
909 Buflen : constant C.int := Netdb_Buffer_Size;
910 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
911 Res : aliased Hostent;
915 if Safe_Gethostbyname
916 (HN, Res'Access, Buf'Address, Buflen, Err'Access) /= 0
918 Raise_Host_Error (Integer (Err));
921 return To_Host_Entry (Res);
923 end Get_Host_By_Name;
929 function Get_Peer_Name (Socket : Socket_Type) return Sock_Addr_Type is
930 Sin : aliased Sockaddr_In;
931 Len : aliased C.int := Sin'Size / 8;
932 Res : Sock_Addr_Type (Family_Inet);
935 if C_Getpeername (C.int (Socket), Sin'Address, Len'Access) = Failure then
936 Raise_Socket_Error (Socket_Errno);
939 To_Inet_Addr (Sin.Sin_Addr, Res.Addr);
940 Res.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
945 -------------------------
946 -- Get_Service_By_Name --
947 -------------------------
949 function Get_Service_By_Name
951 Protocol : String) return Service_Entry_Type
953 SN : constant C.char_array := C.To_C (Name);
954 SP : constant C.char_array := C.To_C (Protocol);
955 Buflen : constant C.int := Netdb_Buffer_Size;
956 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
957 Res : aliased Servent;
960 if Safe_Getservbyname (SN, SP, Res'Access, Buf'Address, Buflen) /= 0 then
961 raise Service_Error with "Service not found";
964 -- Translate from the C format to the API format
966 return To_Service_Entry (Res);
967 end Get_Service_By_Name;
969 -------------------------
970 -- Get_Service_By_Port --
971 -------------------------
973 function Get_Service_By_Port
975 Protocol : String) return Service_Entry_Type
977 SP : constant C.char_array := C.To_C (Protocol);
978 Buflen : constant C.int := Netdb_Buffer_Size;
979 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
980 Res : aliased Servent;
983 if Safe_Getservbyport
984 (C.int (Short_To_Network (C.unsigned_short (Port))), SP,
985 Res'Access, Buf'Address, Buflen) /= 0
987 raise Service_Error with "Service not found";
990 -- Translate from the C format to the API format
992 return To_Service_Entry (Res);
993 end Get_Service_By_Port;
995 ---------------------
996 -- Get_Socket_Name --
997 ---------------------
999 function Get_Socket_Name
1000 (Socket : Socket_Type) return Sock_Addr_Type
1002 Sin : aliased Sockaddr_In;
1003 Len : aliased C.int := Sin'Size / 8;
1005 Addr : Sock_Addr_Type := No_Sock_Addr;
1008 Res := C_Getsockname (C.int (Socket), Sin'Address, Len'Access);
1010 if Res /= Failure then
1011 To_Inet_Addr (Sin.Sin_Addr, Addr.Addr);
1012 Addr.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
1016 end Get_Socket_Name;
1018 -----------------------
1019 -- Get_Socket_Option --
1020 -----------------------
1022 function Get_Socket_Option
1023 (Socket : Socket_Type;
1024 Level : Level_Type := Socket_Level;
1025 Name : Option_Name) return Option_Type
1027 use type C.unsigned_char;
1029 V8 : aliased Two_Ints;
1031 V1 : aliased C.unsigned_char;
1032 VT : aliased Timeval;
1033 Len : aliased C.int;
1034 Add : System.Address;
1036 Opt : Option_Type (Name);
1040 when Multicast_Loop |
1042 Receive_Packet_Info =>
1077 if Res = Failure then
1078 Raise_Socket_Error (Socket_Errno);
1086 Opt.Enabled := (V4 /= 0);
1089 Opt.Enabled := (V8 (V8'First) /= 0);
1090 Opt.Seconds := Natural (V8 (V8'Last));
1094 Opt.Size := Natural (V4);
1097 Opt.Error := Resolve_Error (Integer (V4));
1099 when Add_Membership |
1101 To_Inet_Addr (To_In_Addr (V8 (V8'First)), Opt.Multicast_Address);
1102 To_Inet_Addr (To_In_Addr (V8 (V8'Last)), Opt.Local_Interface);
1104 when Multicast_If =>
1105 To_Inet_Addr (To_In_Addr (V4), Opt.Outgoing_If);
1107 when Multicast_TTL =>
1108 Opt.Time_To_Live := Integer (V1);
1110 when Multicast_Loop |
1111 Receive_Packet_Info =>
1112 Opt.Enabled := (V1 /= 0);
1116 Opt.Timeout := To_Duration (VT);
1120 end Get_Socket_Option;
1126 function Host_Name return String is
1127 Name : aliased C.char_array (1 .. 64);
1131 Res := C_Gethostname (Name'Address, Name'Length);
1133 if Res = Failure then
1134 Raise_Socket_Error (Socket_Errno);
1137 return C.To_Ada (Name);
1145 (Val : Inet_Addr_VN_Type;
1146 Hex : Boolean := False) return String
1148 -- The largest Inet_Addr_Comp_Type image occurs with IPv4. It
1149 -- has at most a length of 3 plus one '.' character.
1151 Buffer : String (1 .. 4 * Val'Length);
1152 Length : Natural := 1;
1153 Separator : Character;
1155 procedure Img10 (V : Inet_Addr_Comp_Type);
1156 -- Append to Buffer image of V in decimal format
1158 procedure Img16 (V : Inet_Addr_Comp_Type);
1159 -- Append to Buffer image of V in hexadecimal format
1165 procedure Img10 (V : Inet_Addr_Comp_Type) is
1166 Img : constant String := V'Img;
1167 Len : constant Natural := Img'Length - 1;
1169 Buffer (Length .. Length + Len - 1) := Img (2 .. Img'Last);
1170 Length := Length + Len;
1177 procedure Img16 (V : Inet_Addr_Comp_Type) is
1179 Buffer (Length) := Hex_To_Char (Natural (V / 16) + 1);
1180 Buffer (Length + 1) := Hex_To_Char (Natural (V mod 16) + 1);
1181 Length := Length + 2;
1184 -- Start of processing for Image
1193 for J in Val'Range loop
1200 if J /= Val'Last then
1201 Buffer (Length) := Separator;
1202 Length := Length + 1;
1206 return Buffer (1 .. Length - 1);
1213 function Image (Value : Inet_Addr_Type) return String is
1215 if Value.Family = Family_Inet then
1216 return Image (Inet_Addr_VN_Type (Value.Sin_V4), Hex => False);
1218 return Image (Inet_Addr_VN_Type (Value.Sin_V6), Hex => True);
1226 function Image (Value : Sock_Addr_Type) return String is
1227 Port : constant String := Value.Port'Img;
1229 return Image (Value.Addr) & ':' & Port (2 .. Port'Last);
1236 function Image (Socket : Socket_Type) return String is
1245 function Inet_Addr (Image : String) return Inet_Addr_Type is
1246 use Interfaces.C.Strings;
1250 Result : Inet_Addr_Type;
1253 -- Special case for the all-ones broadcast address: this address has the
1254 -- same in_addr_t value as Failure, and thus cannot be properly returned
1257 if Image = "255.255.255.255" then
1258 return Broadcast_Inet_Addr;
1260 -- Special case for an empty Image as on some platforms (e.g. Windows)
1261 -- calling Inet_Addr("") will not return an error.
1263 elsif Image = "" then
1264 Raise_Socket_Error (Constants.EINVAL);
1267 Img := New_String (Image);
1268 Res := C_Inet_Addr (Img);
1271 if Res = Failure then
1272 Raise_Socket_Error (Constants.EINVAL);
1275 To_Inet_Addr (To_In_Addr (Res), Result);
1283 procedure Initialize (Process_Blocking_IO : Boolean) is
1284 Expected : constant Boolean := not Constants.Thread_Blocking_IO;
1286 if Process_Blocking_IO /= Expected then
1287 raise Socket_Error with
1288 "incorrect Process_Blocking_IO setting, expected " & Expected'Img;
1298 procedure Initialize is
1300 if not Initialized then
1301 Initialized := True;
1310 function Is_Empty (Item : Socket_Set_Type) return Boolean is
1312 return Item.Last = No_Socket;
1319 function Is_IP_Address (Name : String) return Boolean is
1321 for J in Name'Range loop
1323 and then Name (J) not in '0' .. '9'
1337 (Item : Socket_Set_Type;
1338 Socket : Socket_Type) return Boolean
1341 return Item.Last /= No_Socket
1342 and then Socket <= Item.Last
1343 and then Is_Socket_In_Set (Item.Set, C.int (Socket)) /= 0;
1350 procedure Listen_Socket
1351 (Socket : Socket_Type;
1352 Length : Natural := 15)
1354 Res : constant C.int := C_Listen (C.int (Socket), C.int (Length));
1356 if Res = Failure then
1357 Raise_Socket_Error (Socket_Errno);
1365 procedure Narrow (Item : in out Socket_Set_Type) is
1366 Last : aliased C.int := C.int (Item.Last);
1368 if Item.Set /= No_Fd_Set_Access then
1369 Last_Socket_In_Set (Item.Set, Last'Unchecked_Access);
1370 Item.Last := Socket_Type (Last);
1378 function Official_Name (E : Host_Entry_Type) return String is
1380 return To_String (E.Official);
1387 function Official_Name (S : Service_Entry_Type) return String is
1389 return To_String (S.Official);
1392 --------------------
1393 -- Wait_On_Socket --
1394 --------------------
1396 procedure Wait_On_Socket
1397 (Socket : Socket_Type;
1399 Timeout : Selector_Duration;
1400 Selector : access Selector_Type := null;
1401 Status : out Selector_Status)
1403 type Local_Selector_Access is access Selector_Type;
1404 for Local_Selector_Access'Storage_Size use Selector_Type'Size;
1406 S : Selector_Access;
1407 -- Selector to use for waiting
1409 R_Fd_Set : Socket_Set_Type;
1410 W_Fd_Set : Socket_Set_Type;
1411 -- Socket sets, empty at elaboration
1414 -- Create selector if not provided by the user
1416 if Selector = null then
1418 Local_S : constant Local_Selector_Access := new Selector_Type;
1420 S := Local_S.all'Unchecked_Access;
1421 Create_Selector (S.all);
1425 S := Selector.all'Access;
1429 Set (R_Fd_Set, Socket);
1431 Set (W_Fd_Set, Socket);
1434 Check_Selector (S.all, R_Fd_Set, W_Fd_Set, Status, Timeout);
1436 -- Cleanup actions (required in all cases to avoid memory leaks)
1444 if Selector = null then
1445 Close_Selector (S.all);
1453 function Port_Number (S : Service_Entry_Type) return Port_Type is
1462 function Protocol_Name (S : Service_Entry_Type) return String is
1464 return To_String (S.Protocol);
1467 ----------------------
1468 -- Raise_Host_Error --
1469 ----------------------
1471 procedure Raise_Host_Error (H_Error : Integer) is
1473 raise Host_Error with
1474 Err_Code_Image (H_Error)
1475 & C.Strings.Value (Host_Error_Messages.Host_Error_Message (H_Error));
1476 end Raise_Host_Error;
1478 ------------------------
1479 -- Raise_Socket_Error --
1480 ------------------------
1482 procedure Raise_Socket_Error (Error : Integer) is
1483 use type C.Strings.chars_ptr;
1485 raise Socket_Error with
1486 Err_Code_Image (Error)
1487 & C.Strings.Value (Socket_Error_Message (Error));
1488 end Raise_Socket_Error;
1495 (Stream : in out Datagram_Socket_Stream_Type;
1496 Item : out Ada.Streams.Stream_Element_Array;
1497 Last : out Ada.Streams.Stream_Element_Offset)
1499 First : Ada.Streams.Stream_Element_Offset := Item'First;
1500 Index : Ada.Streams.Stream_Element_Offset := First - 1;
1501 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
1507 Item (First .. Max),
1513 -- Exit when all or zero data received. Zero means that the socket
1516 exit when Index < First or else Index = Max;
1527 (Stream : in out Stream_Socket_Stream_Type;
1528 Item : out Ada.Streams.Stream_Element_Array;
1529 Last : out Ada.Streams.Stream_Element_Offset)
1531 pragma Warnings (Off, Stream);
1533 First : Ada.Streams.Stream_Element_Offset := Item'First;
1534 Index : Ada.Streams.Stream_Element_Offset := First - 1;
1535 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
1539 Receive_Socket (Stream.Socket, Item (First .. Max), Index);
1542 -- Exit when all or zero data received. Zero means that the socket
1545 exit when Index < First or else Index = Max;
1551 --------------------
1552 -- Receive_Socket --
1553 --------------------
1555 procedure Receive_Socket
1556 (Socket : Socket_Type;
1557 Item : out Ada.Streams.Stream_Element_Array;
1558 Last : out Ada.Streams.Stream_Element_Offset;
1559 Flags : Request_Flag_Type := No_Request_Flag)
1565 C_Recv (C.int (Socket), Item'Address, Item'Length, To_Int (Flags));
1567 if Res = Failure then
1568 Raise_Socket_Error (Socket_Errno);
1571 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1574 --------------------
1575 -- Receive_Socket --
1576 --------------------
1578 procedure Receive_Socket
1579 (Socket : Socket_Type;
1580 Item : out Ada.Streams.Stream_Element_Array;
1581 Last : out Ada.Streams.Stream_Element_Offset;
1582 From : out Sock_Addr_Type;
1583 Flags : Request_Flag_Type := No_Request_Flag)
1586 Sin : aliased Sockaddr_In;
1587 Len : aliased C.int := Sin'Size / 8;
1596 Sin'Unchecked_Access,
1599 if Res = Failure then
1600 Raise_Socket_Error (Socket_Errno);
1603 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1605 To_Inet_Addr (Sin.Sin_Addr, From.Addr);
1606 From.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
1613 function Resolve_Error
1614 (Error_Value : Integer;
1615 From_Errno : Boolean := True) return Error_Type
1617 use GNAT.Sockets.Constants;
1620 if not From_Errno then
1622 when Constants.HOST_NOT_FOUND => return Unknown_Host;
1623 when Constants.TRY_AGAIN => return Host_Name_Lookup_Failure;
1624 when Constants.NO_RECOVERY => return Non_Recoverable_Error;
1625 when Constants.NO_DATA => return Unknown_Server_Error;
1626 when others => return Cannot_Resolve_Error;
1631 when ENOERROR => return Success;
1632 when EACCES => return Permission_Denied;
1633 when EADDRINUSE => return Address_Already_In_Use;
1634 when EADDRNOTAVAIL => return Cannot_Assign_Requested_Address;
1635 when EAFNOSUPPORT => return
1636 Address_Family_Not_Supported_By_Protocol;
1637 when EALREADY => return Operation_Already_In_Progress;
1638 when EBADF => return Bad_File_Descriptor;
1639 when ECONNABORTED => return Software_Caused_Connection_Abort;
1640 when ECONNREFUSED => return Connection_Refused;
1641 when ECONNRESET => return Connection_Reset_By_Peer;
1642 when EDESTADDRREQ => return Destination_Address_Required;
1643 when EFAULT => return Bad_Address;
1644 when EHOSTDOWN => return Host_Is_Down;
1645 when EHOSTUNREACH => return No_Route_To_Host;
1646 when EINPROGRESS => return Operation_Now_In_Progress;
1647 when EINTR => return Interrupted_System_Call;
1648 when EINVAL => return Invalid_Argument;
1649 when EIO => return Input_Output_Error;
1650 when EISCONN => return Transport_Endpoint_Already_Connected;
1651 when ELOOP => return Too_Many_Symbolic_Links;
1652 when EMFILE => return Too_Many_Open_Files;
1653 when EMSGSIZE => return Message_Too_Long;
1654 when ENAMETOOLONG => return File_Name_Too_Long;
1655 when ENETDOWN => return Network_Is_Down;
1656 when ENETRESET => return
1657 Network_Dropped_Connection_Because_Of_Reset;
1658 when ENETUNREACH => return Network_Is_Unreachable;
1659 when ENOBUFS => return No_Buffer_Space_Available;
1660 when ENOPROTOOPT => return Protocol_Not_Available;
1661 when ENOTCONN => return Transport_Endpoint_Not_Connected;
1662 when ENOTSOCK => return Socket_Operation_On_Non_Socket;
1663 when EOPNOTSUPP => return Operation_Not_Supported;
1664 when EPFNOSUPPORT => return Protocol_Family_Not_Supported;
1665 when EPROTONOSUPPORT => return Protocol_Not_Supported;
1666 when EPROTOTYPE => return Protocol_Wrong_Type_For_Socket;
1667 when ESHUTDOWN => return
1668 Cannot_Send_After_Transport_Endpoint_Shutdown;
1669 when ESOCKTNOSUPPORT => return Socket_Type_Not_Supported;
1670 when ETIMEDOUT => return Connection_Timed_Out;
1671 when ETOOMANYREFS => return Too_Many_References;
1672 when EWOULDBLOCK => return Resource_Temporarily_Unavailable;
1673 when others => null;
1676 return Cannot_Resolve_Error;
1679 -----------------------
1680 -- Resolve_Exception --
1681 -----------------------
1683 function Resolve_Exception
1684 (Occurrence : Exception_Occurrence) return Error_Type
1686 Id : constant Exception_Id := Exception_Identity (Occurrence);
1687 Msg : constant String := Exception_Message (Occurrence);
1694 while First <= Msg'Last
1695 and then Msg (First) not in '0' .. '9'
1700 if First > Msg'Last then
1701 return Cannot_Resolve_Error;
1705 while Last < Msg'Last
1706 and then Msg (Last + 1) in '0' .. '9'
1711 Val := Integer'Value (Msg (First .. Last));
1713 if Id = Socket_Error_Id then
1714 return Resolve_Error (Val);
1715 elsif Id = Host_Error_Id then
1716 return Resolve_Error (Val, False);
1718 return Cannot_Resolve_Error;
1720 end Resolve_Exception;
1722 --------------------
1723 -- Receive_Vector --
1724 --------------------
1726 procedure Receive_Vector
1727 (Socket : Socket_Type;
1728 Vector : Vector_Type;
1729 Count : out Ada.Streams.Stream_Element_Count)
1740 if Res = Failure then
1741 Raise_Socket_Error (Socket_Errno);
1744 Count := Ada.Streams.Stream_Element_Count (Res);
1751 procedure Send_Socket
1752 (Socket : Socket_Type;
1753 Item : Ada.Streams.Stream_Element_Array;
1754 Last : out Ada.Streams.Stream_Element_Offset;
1755 Flags : Request_Flag_Type := No_Request_Flag)
1765 Set_Forced_Flags (To_Int (Flags)));
1767 if Res = Failure then
1768 Raise_Socket_Error (Socket_Errno);
1771 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1778 procedure Send_Socket
1779 (Socket : Socket_Type;
1780 Item : Ada.Streams.Stream_Element_Array;
1781 Last : out Ada.Streams.Stream_Element_Offset;
1782 To : Sock_Addr_Type;
1783 Flags : Request_Flag_Type := No_Request_Flag)
1786 Sin : aliased Sockaddr_In;
1787 Len : constant C.int := Sin'Size / 8;
1790 Set_Family (Sin.Sin_Family, 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)));
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_Fd_Set_Access then
1862 Item.Set := New_Socket_Set (No_Fd_Set_Access);
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_Ints;
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 Receive_Packet_Info =>
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 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)));
2132 raise Socket_Error with "IPv6 not supported";
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 := 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 := C.Strings.Value (E.S_Proto);
2201 Result : Service_Entry_Type (Aliases_Length => Aliases'Length - 1);
2202 -- The last element is a null pointer
2208 Result.Official := To_Name (Official);
2210 Source := Aliases'First;
2211 Target := Result.Aliases'First;
2212 while Target <= Result.Aliases_Length loop
2213 Result.Aliases (Target) :=
2214 To_Name (C.Strings.Value (Aliases (Source)));
2215 Source := Source + 1;
2216 Target := Target + 1;
2220 Port_Type (Network_To_Short (C.unsigned_short (E.S_Port)));
2222 Result.Protocol := To_Name (Protocol);
2224 end To_Service_Entry;
2230 function To_String (HN : Name_Type) return String is
2232 return HN.Name (1 .. HN.Length);
2239 function To_Timeval (Val : Timeval_Duration) return Timeval is
2244 -- If zero, set result as zero (otherwise it gets rounded down to -1)
2250 -- Normal case where we do round down
2253 S := time_t (Val - 0.5);
2254 uS := suseconds_t (1_000_000 * (Val - Selector_Duration (S)));
2265 (Stream : in out Datagram_Socket_Stream_Type;
2266 Item : Ada.Streams.Stream_Element_Array)
2268 pragma Warnings (Off, Stream);
2270 First : Ada.Streams.Stream_Element_Offset := Item'First;
2271 Index : Ada.Streams.Stream_Element_Offset := First - 1;
2272 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
2278 Item (First .. Max),
2282 -- Exit when all or zero data sent. Zero means that the socket has
2283 -- been closed by peer.
2285 exit when Index < First or else Index = Max;
2290 if Index /= Max then
2300 (Stream : in out Stream_Socket_Stream_Type;
2301 Item : Ada.Streams.Stream_Element_Array)
2303 pragma Warnings (Off, Stream);
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