1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S --
9 -- Copyright (C) 2001-2006, 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
);
564 -- Reset R_Sig_Socket and W_Sig_Socket to No_Socket to ensure that any
565 -- (errneous) subsequent attempt to use this selector properly fails.
567 Selector
.R_Sig_Socket
:= No_Socket
;
568 Selector
.W_Sig_Socket
:= No_Socket
;
575 procedure Close_Socket
(Socket
: Socket_Type
) is
579 Res
:= C_Close
(C
.int
(Socket
));
581 if Res
= Failure
then
582 Raise_Socket_Error
(Socket_Errno
);
590 procedure Connect_Socket
591 (Socket
: Socket_Type
;
592 Server
: in out Sock_Addr_Type
)
595 Sin
: aliased Sockaddr_In
;
596 Len
: constant C
.int
:= Sin
'Size / 8;
599 if Server
.Family
= Family_Inet6
then
603 Set_Length
(Sin
'Unchecked_Access, Len
);
604 Set_Family
(Sin
'Unchecked_Access, Families
(Server
.Family
));
605 Set_Address
(Sin
'Unchecked_Access, To_In_Addr
(Server
.Addr
));
607 (Sin
'Unchecked_Access,
608 Short_To_Network
(C
.unsigned_short
(Server
.Port
)));
610 Res
:= C_Connect
(C
.int
(Socket
), Sin
'Address, Len
);
612 if Res
= Failure
then
613 Raise_Socket_Error
(Socket_Errno
);
621 procedure Control_Socket
622 (Socket
: Socket_Type
;
623 Request
: in out Request_Type
)
630 when Non_Blocking_IO
=>
631 Arg
:= C
.int
(Boolean'Pos (Request
.Enabled
));
633 when N_Bytes_To_Read
=>
640 Requests
(Request
.Name
),
641 Arg
'Unchecked_Access);
643 if Res
= Failure
then
644 Raise_Socket_Error
(Socket_Errno
);
648 when Non_Blocking_IO
=>
651 when N_Bytes_To_Read
=>
652 Request
.Size
:= Natural (Arg
);
661 (Source
: Socket_Set_Type
;
662 Target
: in out Socket_Set_Type
)
666 if Source
.Last
/= No_Socket
then
667 Target
.Set
:= New_Socket_Set
(Source
.Set
);
668 Target
.Last
:= Source
.Last
;
672 ---------------------
673 -- Create_Selector --
674 ---------------------
676 procedure Create_Selector
(Selector
: out Selector_Type
) is
681 Sin
: aliased Sockaddr_In
;
682 Len
: aliased C
.int
:= Sin
'Size / 8;
686 -- We open two signalling sockets. One of them is used to send data to
687 -- the other, which is included in a C_Select socket set. The
688 -- communication is used to force the call to C_Select to complete, and
689 -- the waiting task to resume its execution.
691 -- Create a listening socket
693 S0
:= C_Socket
(Constants
.AF_INET
, Constants
.SOCK_STREAM
, 0);
696 Raise_Socket_Error
(Socket_Errno
);
699 -- Bind the socket to any unused port on localhost
701 Sin
.Sin_Addr
.S_B1
:= 127;
702 Sin
.Sin_Addr
.S_B2
:= 0;
703 Sin
.Sin_Addr
.S_B3
:= 0;
704 Sin
.Sin_Addr
.S_B4
:= 1;
707 Res
:= C_Bind
(S0
, Sin
'Address, Len
);
709 if Res
= Failure
then
712 Raise_Socket_Error
(Err
);
715 -- Get the port used by the socket
717 Res
:= C_Getsockname
(S0
, Sin
'Address, Len
'Access);
719 if Res
= Failure
then
722 Raise_Socket_Error
(Err
);
725 -- Set backlog to 1 to guarantee that exactly one call to connect(2)
728 Res
:= C_Listen
(S0
, 1);
730 if Res
= Failure
then
733 Raise_Socket_Error
(Err
);
736 S1
:= C_Socket
(Constants
.AF_INET
, Constants
.SOCK_STREAM
, 0);
741 Raise_Socket_Error
(Err
);
744 -- Do a connect and accept the connection
746 Res
:= C_Connect
(S1
, Sin
'Address, Len
);
748 if Res
= Failure
then
752 Raise_Socket_Error
(Err
);
755 -- Since the call to connect(2) has suceeded and the backlog limit on
756 -- the listening socket is 1, we know that there is now exactly one
757 -- pending connection on S0, which is the one from S1.
759 S2
:= C_Accept
(S0
, Sin
'Address, Len
'Access);
765 Raise_Socket_Error
(Err
);
770 if Res
= Failure
then
771 Raise_Socket_Error
(Socket_Errno
);
774 Selector
.R_Sig_Socket
:= Socket_Type
(S1
);
775 Selector
.W_Sig_Socket
:= Socket_Type
(S2
);
782 procedure Create_Socket
783 (Socket
: out Socket_Type
;
784 Family
: Family_Type
:= Family_Inet
;
785 Mode
: Mode_Type
:= Socket_Stream
)
790 Res
:= C_Socket
(Families
(Family
), Modes
(Mode
), 0);
792 if Res
= Failure
then
793 Raise_Socket_Error
(Socket_Errno
);
796 Socket
:= Socket_Type
(Res
);
803 procedure Empty
(Item
: in out Socket_Set_Type
) is
805 if Item
.Set
/= No_Socket_Set
then
806 Free_Socket_Set
(Item
.Set
);
807 Item
.Set
:= No_Socket_Set
;
810 Item
.Last
:= No_Socket
;
817 function Err_Code_Image
(E
: Integer) return String is
818 Msg
: String := E
'Img & "] ";
820 Msg
(Msg
'First) := '[';
828 procedure Finalize
is
843 (Item
: in out Socket_Set_Type
;
844 Socket
: out Socket_Type
)
847 L
: aliased C
.int
:= C
.int
(Item
.Last
);
850 if Item
.Last
/= No_Socket
then
852 (Item
.Set
, L
'Unchecked_Access, S
'Unchecked_Access);
853 Item
.Last
:= Socket_Type
(L
);
854 Socket
:= Socket_Type
(S
);
864 function Get_Address
(Stream
: Stream_Access
) return Sock_Addr_Type
is
866 if Stream
= null then
868 elsif Stream
.all in Datagram_Socket_Stream_Type
then
869 return Datagram_Socket_Stream_Type
(Stream
.all).From
;
871 return Get_Peer_Name
(Stream_Socket_Stream_Type
(Stream
.all).Socket
);
875 -------------------------
876 -- Get_Host_By_Address --
877 -------------------------
879 function Get_Host_By_Address
880 (Address
: Inet_Addr_Type
;
881 Family
: Family_Type
:= Family_Inet
) return Host_Entry_Type
883 pragma Unreferenced
(Family
);
885 HA
: aliased In_Addr
:= To_In_Addr
(Address
);
886 Res
: Hostent_Access
;
890 -- This C function is not always thread-safe. Protect against
891 -- concurrent access.
894 Res
:= C_Gethostbyaddr
(HA
'Address, HA
'Size / 8, Constants
.AF_INET
);
899 Raise_Host_Error
(Err
);
902 -- Translate from the C format to the API format
905 HE
: constant Host_Entry_Type
:= To_Host_Entry
(Res
.all);
911 end Get_Host_By_Address
;
913 ----------------------
914 -- Get_Host_By_Name --
915 ----------------------
917 function Get_Host_By_Name
(Name
: String) return Host_Entry_Type
is
918 HN
: constant C
.char_array
:= C
.To_C
(Name
);
919 Res
: Hostent_Access
;
923 -- Detect IP address name and redirect to Inet_Addr
925 if Is_IP_Address
(Name
) then
926 return Get_Host_By_Address
(Inet_Addr
(Name
));
929 -- This C function is not always thread-safe. Protect against
930 -- concurrent access.
933 Res
:= C_Gethostbyname
(HN
);
938 Raise_Host_Error
(Err
);
941 -- Translate from the C format to the API format
944 HE
: constant Host_Entry_Type
:= To_Host_Entry
(Res
.all);
949 end Get_Host_By_Name
;
955 function Get_Peer_Name
(Socket
: Socket_Type
) return Sock_Addr_Type
is
956 Sin
: aliased Sockaddr_In
;
957 Len
: aliased C
.int
:= Sin
'Size / 8;
958 Res
: Sock_Addr_Type
(Family_Inet
);
961 if C_Getpeername
(C
.int
(Socket
), Sin
'Address, Len
'Access) = Failure
then
962 Raise_Socket_Error
(Socket_Errno
);
965 To_Inet_Addr
(Sin
.Sin_Addr
, Res
.Addr
);
966 Res
.Port
:= Port_Type
(Network_To_Short
(Sin
.Sin_Port
));
971 -------------------------
972 -- Get_Service_By_Name --
973 -------------------------
975 function Get_Service_By_Name
977 Protocol
: String) return Service_Entry_Type
979 SN
: constant C
.char_array
:= C
.To_C
(Name
);
980 SP
: constant C
.char_array
:= C
.To_C
(Protocol
);
981 Res
: Servent_Access
;
984 -- This C function is not always thread-safe. Protect against
985 -- concurrent access.
988 Res
:= C_Getservbyname
(SN
, SP
);
992 Ada
.Exceptions
.Raise_Exception
993 (Service_Error
'Identity, "Service not found");
996 -- Translate from the C format to the API format
999 SE
: constant Service_Entry_Type
:= To_Service_Entry
(Res
.all);
1005 end Get_Service_By_Name
;
1007 -------------------------
1008 -- Get_Service_By_Port --
1009 -------------------------
1011 function Get_Service_By_Port
1013 Protocol
: String) return Service_Entry_Type
1015 SP
: constant C
.char_array
:= C
.To_C
(Protocol
);
1016 Res
: Servent_Access
;
1019 -- This C function is not always thread-safe. Protect against
1020 -- concurrent access.
1023 Res
:= C_Getservbyport
1024 (C
.int
(Short_To_Network
(C
.unsigned_short
(Port
))), SP
);
1028 Ada
.Exceptions
.Raise_Exception
1029 (Service_Error
'Identity, "Service not found");
1032 -- Translate from the C format to the API format
1035 SE
: constant Service_Entry_Type
:= To_Service_Entry
(Res
.all);
1041 end Get_Service_By_Port
;
1043 ---------------------
1044 -- Get_Socket_Name --
1045 ---------------------
1047 function Get_Socket_Name
1048 (Socket
: Socket_Type
) return Sock_Addr_Type
1050 Sin
: aliased Sockaddr_In
;
1051 Len
: aliased C
.int
:= Sin
'Size / 8;
1053 Addr
: Sock_Addr_Type
:= No_Sock_Addr
;
1056 Res
:= C_Getsockname
(C
.int
(Socket
), Sin
'Address, Len
'Access);
1057 if Res
/= Failure
then
1058 To_Inet_Addr
(Sin
.Sin_Addr
, Addr
.Addr
);
1059 Addr
.Port
:= Port_Type
(Network_To_Short
(Sin
.Sin_Port
));
1063 end Get_Socket_Name
;
1065 -----------------------
1066 -- Get_Socket_Option --
1067 -----------------------
1069 function Get_Socket_Option
1070 (Socket
: Socket_Type
;
1071 Level
: Level_Type
:= Socket_Level
;
1072 Name
: Option_Name
) return Option_Type
1074 use type C
.unsigned_char
;
1076 V8
: aliased Two_Int
;
1078 V1
: aliased C
.unsigned_char
;
1079 VT
: aliased Timeval
;
1080 Len
: aliased C
.int
;
1081 Add
: System
.Address
;
1083 Opt
: Option_Type
(Name
);
1087 when Multicast_Loop |
1121 Add
, Len
'Unchecked_Access);
1123 if Res
= Failure
then
1124 Raise_Socket_Error
(Socket_Errno
);
1132 Opt
.Enabled
:= (V4
/= 0);
1135 Opt
.Enabled
:= (V8
(V8
'First) /= 0);
1136 Opt
.Seconds
:= Natural (V8
(V8
'Last));
1140 Opt
.Size
:= Natural (V4
);
1143 Opt
.Error
:= Resolve_Error
(Integer (V4
));
1145 when Add_Membership |
1147 To_Inet_Addr
(To_In_Addr
(V8
(V8
'First)), Opt
.Multicast_Address
);
1148 To_Inet_Addr
(To_In_Addr
(V8
(V8
'Last)), Opt
.Local_Interface
);
1150 when Multicast_If
=>
1151 To_Inet_Addr
(To_In_Addr
(V4
), Opt
.Outgoing_If
);
1153 when Multicast_TTL
=>
1154 Opt
.Time_To_Live
:= Integer (V1
);
1156 when Multicast_Loop
=>
1157 Opt
.Enabled
:= (V1
/= 0);
1161 Opt
.Timeout
:= To_Duration
(VT
);
1166 end Get_Socket_Option
;
1172 function Host_Name
return String is
1173 Name
: aliased C
.char_array
(1 .. 64);
1177 Res
:= C_Gethostname
(Name
'Address, Name
'Length);
1179 if Res
= Failure
then
1180 Raise_Socket_Error
(Socket_Errno
);
1183 return C
.To_Ada
(Name
);
1191 (Val
: Inet_Addr_VN_Type
;
1192 Hex
: Boolean := False) return String
1194 -- The largest Inet_Addr_Comp_Type image occurs with IPv4. It
1195 -- has at most a length of 3 plus one '.' character.
1197 Buffer
: String (1 .. 4 * Val
'Length);
1198 Length
: Natural := 1;
1199 Separator
: Character;
1201 procedure Img10
(V
: Inet_Addr_Comp_Type
);
1202 -- Append to Buffer image of V in decimal format
1204 procedure Img16
(V
: Inet_Addr_Comp_Type
);
1205 -- Append to Buffer image of V in hexadecimal format
1211 procedure Img10
(V
: Inet_Addr_Comp_Type
) is
1212 Img
: constant String := V
'Img;
1213 Len
: constant Natural := Img
'Length - 1;
1215 Buffer
(Length
.. Length
+ Len
- 1) := Img
(2 .. Img
'Last);
1216 Length
:= Length
+ Len
;
1223 procedure Img16
(V
: Inet_Addr_Comp_Type
) is
1225 Buffer
(Length
) := Hex_To_Char
(Natural (V
/ 16) + 1);
1226 Buffer
(Length
+ 1) := Hex_To_Char
(Natural (V
mod 16) + 1);
1227 Length
:= Length
+ 2;
1230 -- Start of processing for Image
1239 for J
in Val
'Range loop
1246 if J
/= Val
'Last then
1247 Buffer
(Length
) := Separator
;
1248 Length
:= Length
+ 1;
1252 return Buffer
(1 .. Length
- 1);
1259 function Image
(Value
: Inet_Addr_Type
) return String is
1261 if Value
.Family
= Family_Inet
then
1262 return Image
(Inet_Addr_VN_Type
(Value
.Sin_V4
), Hex
=> False);
1264 return Image
(Inet_Addr_VN_Type
(Value
.Sin_V6
), Hex
=> True);
1272 function Image
(Value
: Sock_Addr_Type
) return String is
1273 Port
: constant String := Value
.Port
'Img;
1275 return Image
(Value
.Addr
) & ':' & Port
(2 .. Port
'Last);
1282 function Image
(Socket
: Socket_Type
) return String is
1291 function Inet_Addr
(Image
: String) return Inet_Addr_Type
is
1292 use Interfaces
.C
.Strings
;
1296 Result
: Inet_Addr_Type
;
1299 -- Special case for the all-ones broadcast address: this address
1300 -- has the same in_addr_t value as Failure, and thus cannot be
1301 -- properly returned by inet_addr(3).
1303 if Image
= "255.255.255.255" then
1304 return Broadcast_Inet_Addr
;
1306 -- Special case for an empty Image as on some platforms (e.g. Windows)
1307 -- calling Inet_Addr("") will not return an error.
1309 elsif Image
= "" then
1310 Raise_Socket_Error
(Constants
.EINVAL
);
1313 Img
:= New_String
(Image
);
1314 Res
:= C_Inet_Addr
(Img
);
1317 if Res
= Failure
then
1318 Raise_Socket_Error
(Constants
.EINVAL
);
1321 To_Inet_Addr
(To_In_Addr
(Res
), Result
);
1329 procedure Initialize
(Process_Blocking_IO
: Boolean := False) is
1331 if not Initialized
then
1332 Initialized
:= True;
1333 Thin
.Initialize
(Process_Blocking_IO
);
1341 function Is_Empty
(Item
: Socket_Set_Type
) return Boolean is
1343 return Item
.Last
= No_Socket
;
1350 function Is_IP_Address
(Name
: String) return Boolean is
1352 for J
in Name
'Range loop
1354 and then Name
(J
) not in '0' .. '9'
1368 (Item
: Socket_Set_Type
;
1369 Socket
: Socket_Type
) return Boolean
1372 return Item
.Last
/= No_Socket
1373 and then Socket
<= Item
.Last
1374 and then Is_Socket_In_Set
(Item
.Set
, C
.int
(Socket
)) /= 0;
1381 procedure Listen_Socket
1382 (Socket
: Socket_Type
;
1383 Length
: Positive := 15)
1385 Res
: constant C
.int
:= C_Listen
(C
.int
(Socket
), C
.int
(Length
));
1387 if Res
= Failure
then
1388 Raise_Socket_Error
(Socket_Errno
);
1396 procedure Narrow
(Item
: in out Socket_Set_Type
) is
1397 Last
: aliased C
.int
:= C
.int
(Item
.Last
);
1399 if Item
.Set
/= No_Socket_Set
then
1400 Last_Socket_In_Set
(Item
.Set
, Last
'Unchecked_Access);
1401 Item
.Last
:= Socket_Type
(Last
);
1409 function Official_Name
(E
: Host_Entry_Type
) return String is
1411 return To_String
(E
.Official
);
1418 function Official_Name
(S
: Service_Entry_Type
) return String is
1420 return To_String
(S
.Official
);
1427 function Port_Number
(S
: Service_Entry_Type
) return Port_Type
is
1436 function Protocol_Name
(S
: Service_Entry_Type
) return String is
1438 return To_String
(S
.Protocol
);
1441 ----------------------
1442 -- Raise_Host_Error --
1443 ----------------------
1445 procedure Raise_Host_Error
(H_Error
: Integer) is
1447 function Host_Error_Message
return String;
1448 -- We do not use a C function like strerror because hstrerror that would
1449 -- correspond is obsolete. Return appropriate string for error value.
1451 ------------------------
1452 -- Host_Error_Message --
1453 ------------------------
1455 function Host_Error_Message
return String is
1458 when Constants
.HOST_NOT_FOUND
=> return "Host not found";
1459 when Constants
.TRY_AGAIN
=> return "Try again";
1460 when Constants
.NO_RECOVERY
=> return "No recovery";
1461 when Constants
.NO_DATA
=> return "No address";
1462 when others => return "Unknown error";
1464 end Host_Error_Message
;
1466 -- Start of processing for Raise_Host_Error
1469 Ada
.Exceptions
.Raise_Exception
(Host_Error
'Identity,
1470 Err_Code_Image
(H_Error
)
1471 & Host_Error_Message
);
1472 end Raise_Host_Error
;
1474 ------------------------
1475 -- Raise_Socket_Error --
1476 ------------------------
1478 procedure Raise_Socket_Error
(Error
: Integer) is
1479 use type C
.Strings
.chars_ptr
;
1481 Ada
.Exceptions
.Raise_Exception
(Socket_Error
'Identity,
1482 Err_Code_Image
(Error
)
1483 & C
.Strings
.Value
(Socket_Error_Message
(Error
)));
1484 end Raise_Socket_Error
;
1491 (Stream
: in out Datagram_Socket_Stream_Type
;
1492 Item
: out Ada
.Streams
.Stream_Element_Array
;
1493 Last
: out Ada
.Streams
.Stream_Element_Offset
)
1495 First
: Ada
.Streams
.Stream_Element_Offset
:= Item
'First;
1496 Index
: Ada
.Streams
.Stream_Element_Offset
:= First
- 1;
1497 Max
: constant Ada
.Streams
.Stream_Element_Offset
:= Item
'Last;
1503 Item
(First
.. Max
),
1509 -- Exit when all or zero data received. Zero means that the socket
1512 exit when Index
< First
or else Index
= Max
;
1523 (Stream
: in out Stream_Socket_Stream_Type
;
1524 Item
: out Ada
.Streams
.Stream_Element_Array
;
1525 Last
: out Ada
.Streams
.Stream_Element_Offset
)
1527 First
: Ada
.Streams
.Stream_Element_Offset
:= Item
'First;
1528 Index
: Ada
.Streams
.Stream_Element_Offset
:= First
- 1;
1529 Max
: constant Ada
.Streams
.Stream_Element_Offset
:= Item
'Last;
1533 Receive_Socket
(Stream
.Socket
, Item
(First
.. Max
), Index
);
1536 -- Exit when all or zero data received. Zero means that the socket
1539 exit when Index
< First
or else Index
= Max
;
1545 --------------------
1546 -- Receive_Socket --
1547 --------------------
1549 procedure Receive_Socket
1550 (Socket
: Socket_Type
;
1551 Item
: out Ada
.Streams
.Stream_Element_Array
;
1552 Last
: out Ada
.Streams
.Stream_Element_Offset
;
1553 Flags
: Request_Flag_Type
:= No_Request_Flag
)
1555 use type Ada
.Streams
.Stream_Element_Offset
;
1562 Item
(Item
'First)'Address,
1566 if Res
= Failure
then
1567 Raise_Socket_Error
(Socket_Errno
);
1570 Last
:= Item
'First + Ada
.Streams
.Stream_Element_Offset
(Res
- 1);
1573 --------------------
1574 -- Receive_Socket --
1575 --------------------
1577 procedure Receive_Socket
1578 (Socket
: Socket_Type
;
1579 Item
: out Ada
.Streams
.Stream_Element_Array
;
1580 Last
: out Ada
.Streams
.Stream_Element_Offset
;
1581 From
: out Sock_Addr_Type
;
1582 Flags
: Request_Flag_Type
:= No_Request_Flag
)
1584 use type Ada
.Streams
.Stream_Element_Offset
;
1587 Sin
: aliased Sockaddr_In
;
1588 Len
: aliased C
.int
:= Sin
'Size / 8;
1594 Item
(Item
'First)'Address,
1597 Sin
'Unchecked_Access,
1598 Len
'Unchecked_Access);
1600 if Res
= Failure
then
1601 Raise_Socket_Error
(Socket_Errno
);
1604 Last
:= Item
'First + Ada
.Streams
.Stream_Element_Offset
(Res
- 1);
1606 To_Inet_Addr
(Sin
.Sin_Addr
, From
.Addr
);
1607 From
.Port
:= Port_Type
(Network_To_Short
(Sin
.Sin_Port
));
1614 function Resolve_Error
1615 (Error_Value
: Integer;
1616 From_Errno
: Boolean := True) return Error_Type
1618 use GNAT
.Sockets
.Constants
;
1621 if not From_Errno
then
1623 when Constants
.HOST_NOT_FOUND
=> return Unknown_Host
;
1624 when Constants
.TRY_AGAIN
=> return Host_Name_Lookup_Failure
;
1625 when Constants
.NO_RECOVERY
=>
1626 return Non_Recoverable_Error
;
1627 when Constants
.NO_DATA
=> return Unknown_Server_Error
;
1628 when others => return Cannot_Resolve_Error
;
1633 when ENOERROR
=> return Success
;
1634 when EACCES
=> return Permission_Denied
;
1635 when EADDRINUSE
=> return Address_Already_In_Use
;
1636 when EADDRNOTAVAIL
=> return Cannot_Assign_Requested_Address
;
1637 when EAFNOSUPPORT
=>
1638 return Address_Family_Not_Supported_By_Protocol
;
1639 when EALREADY
=> return Operation_Already_In_Progress
;
1640 when EBADF
=> return Bad_File_Descriptor
;
1641 when ECONNABORTED
=> return Software_Caused_Connection_Abort
;
1642 when ECONNREFUSED
=> return Connection_Refused
;
1643 when ECONNRESET
=> return Connection_Reset_By_Peer
;
1644 when EDESTADDRREQ
=> return Destination_Address_Required
;
1645 when EFAULT
=> return Bad_Address
;
1646 when EHOSTDOWN
=> return Host_Is_Down
;
1647 when EHOSTUNREACH
=> return No_Route_To_Host
;
1648 when EINPROGRESS
=> return Operation_Now_In_Progress
;
1649 when EINTR
=> return Interrupted_System_Call
;
1650 when EINVAL
=> return Invalid_Argument
;
1651 when EIO
=> return Input_Output_Error
;
1652 when EISCONN
=> return Transport_Endpoint_Already_Connected
;
1653 when ELOOP
=> return Too_Many_Symbolic_Links
;
1654 when EMFILE
=> return Too_Many_Open_Files
;
1655 when EMSGSIZE
=> return Message_Too_Long
;
1656 when ENAMETOOLONG
=> return File_Name_Too_Long
;
1657 when ENETDOWN
=> return Network_Is_Down
;
1659 return Network_Dropped_Connection_Because_Of_Reset
;
1660 when ENETUNREACH
=> return Network_Is_Unreachable
;
1661 when ENOBUFS
=> return No_Buffer_Space_Available
;
1662 when ENOPROTOOPT
=> return Protocol_Not_Available
;
1663 when ENOTCONN
=> return Transport_Endpoint_Not_Connected
;
1664 when ENOTSOCK
=> return Socket_Operation_On_Non_Socket
;
1665 when EOPNOTSUPP
=> return Operation_Not_Supported
;
1666 when EPFNOSUPPORT
=> return Protocol_Family_Not_Supported
;
1667 when EPROTONOSUPPORT
=> return Protocol_Not_Supported
;
1668 when EPROTOTYPE
=> return Protocol_Wrong_Type_For_Socket
;
1670 return Cannot_Send_After_Transport_Endpoint_Shutdown
;
1671 when ESOCKTNOSUPPORT
=> return Socket_Type_Not_Supported
;
1672 when ETIMEDOUT
=> return Connection_Timed_Out
;
1673 when ETOOMANYREFS
=> return Too_Many_References
;
1674 when EWOULDBLOCK
=> return Resource_Temporarily_Unavailable
;
1675 when others => null;
1678 return Cannot_Resolve_Error
;
1681 -----------------------
1682 -- Resolve_Exception --
1683 -----------------------
1685 function Resolve_Exception
1686 (Occurrence
: Exception_Occurrence
) return Error_Type
1688 Id
: constant Exception_Id
:= Exception_Identity
(Occurrence
);
1689 Msg
: constant String := Exception_Message
(Occurrence
);
1696 while First
<= Msg
'Last
1697 and then Msg
(First
) not in '0' .. '9'
1702 if First
> Msg
'Last then
1703 return Cannot_Resolve_Error
;
1707 while Last
< Msg
'Last
1708 and then Msg
(Last
+ 1) in '0' .. '9'
1713 Val
:= Integer'Value (Msg
(First
.. Last
));
1715 if Id
= Socket_Error_Id
then
1716 return Resolve_Error
(Val
);
1717 elsif Id
= Host_Error_Id
then
1718 return Resolve_Error
(Val
, False);
1720 return Cannot_Resolve_Error
;
1722 end Resolve_Exception
;
1724 --------------------
1725 -- Receive_Vector --
1726 --------------------
1728 procedure Receive_Vector
1729 (Socket
: Socket_Type
;
1730 Vector
: Vector_Type
;
1731 Count
: out Ada
.Streams
.Stream_Element_Count
)
1739 Vector
(Vector
'First)'Address,
1742 if Res
= Failure
then
1743 Raise_Socket_Error
(Socket_Errno
);
1746 Count
:= Ada
.Streams
.Stream_Element_Count
(Res
);
1753 procedure Send_Socket
1754 (Socket
: Socket_Type
;
1755 Item
: Ada
.Streams
.Stream_Element_Array
;
1756 Last
: out Ada
.Streams
.Stream_Element_Offset
;
1757 Flags
: Request_Flag_Type
:= No_Request_Flag
)
1759 use type Ada
.Streams
.Stream_Element_Offset
;
1767 Item
(Item
'First)'Address,
1769 Set_Forced_Flags
(To_Int
(Flags
)));
1771 if Res
= Failure
then
1772 Raise_Socket_Error
(Socket_Errno
);
1775 Last
:= Item
'First + Ada
.Streams
.Stream_Element_Offset
(Res
- 1);
1782 procedure Send_Socket
1783 (Socket
: Socket_Type
;
1784 Item
: Ada
.Streams
.Stream_Element_Array
;
1785 Last
: out Ada
.Streams
.Stream_Element_Offset
;
1786 To
: Sock_Addr_Type
;
1787 Flags
: Request_Flag_Type
:= No_Request_Flag
)
1789 use type Ada
.Streams
.Stream_Element_Offset
;
1792 Sin
: aliased Sockaddr_In
;
1793 Len
: constant C
.int
:= Sin
'Size / 8;
1796 Set_Length
(Sin
'Unchecked_Access, Len
);
1797 Set_Family
(Sin
'Unchecked_Access, Families
(To
.Family
));
1798 Set_Address
(Sin
'Unchecked_Access, To_In_Addr
(To
.Addr
));
1800 (Sin
'Unchecked_Access,
1801 Short_To_Network
(C
.unsigned_short
(To
.Port
)));
1805 Item
(Item
'First)'Address,
1807 Set_Forced_Flags
(To_Int
(Flags
)),
1808 Sin
'Unchecked_Access,
1811 if Res
= Failure
then
1812 Raise_Socket_Error
(Socket_Errno
);
1815 Last
:= Item
'First + Ada
.Streams
.Stream_Element_Offset
(Res
- 1);
1822 procedure Send_Vector
1823 (Socket
: Socket_Type
;
1824 Vector
: Vector_Type
;
1825 Count
: out Ada
.Streams
.Stream_Element_Count
)
1829 This_Iov_Count
: C
.int
;
1834 while Iov_Count
< Vector
'Length loop
1836 pragma Warnings
(Off
);
1837 -- Following test may be compile time known on some targets
1839 if Vector
'Length - Iov_Count
> Constants
.IOV_MAX
then
1840 This_Iov_Count
:= Constants
.IOV_MAX
;
1842 This_Iov_Count
:= Vector
'Length - Iov_Count
;
1845 pragma Warnings
(On
);
1850 Vector
(Vector
'First + Integer (Iov_Count
))'Address,
1853 if Res
= Failure
then
1854 Raise_Socket_Error
(Socket_Errno
);
1857 Count
:= Count
+ Ada
.Streams
.Stream_Element_Count
(Res
);
1858 Iov_Count
:= Iov_Count
+ This_Iov_Count
;
1866 procedure Set
(Item
: in out Socket_Set_Type
; Socket
: Socket_Type
) is
1868 if Item
.Set
= No_Socket_Set
then
1869 Item
.Set
:= New_Socket_Set
(No_Socket_Set
);
1870 Item
.Last
:= Socket
;
1872 elsif Item
.Last
< Socket
then
1873 Item
.Last
:= Socket
;
1876 Insert_Socket_In_Set
(Item
.Set
, C
.int
(Socket
));
1879 ----------------------
1880 -- Set_Forced_Flags --
1881 ----------------------
1883 function Set_Forced_Flags
(F
: C
.int
) return C
.int
is
1884 use type C
.unsigned
;
1885 function To_unsigned
is
1886 new Ada
.Unchecked_Conversion
(C
.int
, C
.unsigned
);
1888 new Ada
.Unchecked_Conversion
(C
.unsigned
, C
.int
);
1890 return To_int
(To_unsigned
(F
) or Constants
.MSG_Forced_Flags
);
1891 end Set_Forced_Flags
;
1893 -----------------------
1894 -- Set_Socket_Option --
1895 -----------------------
1897 procedure Set_Socket_Option
1898 (Socket
: Socket_Type
;
1899 Level
: Level_Type
:= Socket_Level
;
1900 Option
: Option_Type
)
1902 V8
: aliased Two_Int
;
1904 V1
: aliased C
.unsigned_char
;
1905 VT
: aliased Timeval
;
1907 Add
: System
.Address
:= Null_Address
;
1916 V4
:= C
.int
(Boolean'Pos (Option
.Enabled
));
1921 V8
(V8
'First) := C
.int
(Boolean'Pos (Option
.Enabled
));
1922 V8
(V8
'Last) := C
.int
(Option
.Seconds
);
1928 V4
:= C
.int
(Option
.Size
);
1933 V4
:= C
.int
(Boolean'Pos (True));
1937 when Add_Membership |
1939 V8
(V8
'First) := To_Int
(To_In_Addr
(Option
.Multicast_Address
));
1940 V8
(V8
'Last) := To_Int
(To_In_Addr
(Option
.Local_Interface
));
1944 when Multicast_If
=>
1945 V4
:= To_Int
(To_In_Addr
(Option
.Outgoing_If
));
1949 when Multicast_TTL
=>
1950 V1
:= C
.unsigned_char
(Option
.Time_To_Live
);
1954 when Multicast_Loop
=>
1955 V1
:= C
.unsigned_char
(Boolean'Pos (Option
.Enabled
));
1961 VT
:= To_Timeval
(Option
.Timeout
);
1970 Options
(Option
.Name
),
1973 if Res
= Failure
then
1974 Raise_Socket_Error
(Socket_Errno
);
1976 end Set_Socket_Option
;
1978 ----------------------
1979 -- Short_To_Network --
1980 ----------------------
1982 function Short_To_Network
(S
: C
.unsigned_short
) return C
.unsigned_short
is
1983 use type C
.unsigned_short
;
1986 -- Big-endian case. No conversion needed. On these platforms,
1987 -- htons() defaults to a null procedure.
1989 pragma Warnings
(Off
);
1990 -- Since the test can generate "always True/False" warning
1992 if Default_Bit_Order
= High_Order_First
then
1995 pragma Warnings
(On
);
1997 -- Little-endian case. We must swap the high and low bytes of this
1998 -- short to make the port number network compliant.
2001 return (S
/ 256) + (S
mod 256) * 256;
2003 end Short_To_Network
;
2005 ---------------------
2006 -- Shutdown_Socket --
2007 ---------------------
2009 procedure Shutdown_Socket
2010 (Socket
: Socket_Type
;
2011 How
: Shutmode_Type
:= Shut_Read_Write
)
2016 Res
:= C_Shutdown
(C
.int
(Socket
), Shutmodes
(How
));
2018 if Res
= Failure
then
2019 Raise_Socket_Error
(Socket_Errno
);
2021 end Shutdown_Socket
;
2028 (Socket
: Socket_Type
;
2029 Send_To
: Sock_Addr_Type
) return Stream_Access
2031 S
: Datagram_Socket_Stream_Access
;
2034 S
:= new Datagram_Socket_Stream_Type
;
2037 S
.From
:= Get_Socket_Name
(Socket
);
2038 return Stream_Access
(S
);
2045 function Stream
(Socket
: Socket_Type
) return Stream_Access
is
2046 S
: Stream_Socket_Stream_Access
;
2048 S
:= new Stream_Socket_Stream_Type
;
2050 return Stream_Access
(S
);
2057 function To_C
(Socket
: Socket_Type
) return Integer is
2059 return Integer (Socket
);
2066 function To_Duration
(Val
: Timeval
) return Timeval_Duration
is
2068 return Natural (Val
.Tv_Sec
) * 1.0 + Natural (Val
.Tv_Usec
) * 1.0E-6;
2075 function To_Host_Entry
(E
: Hostent
) return Host_Entry_Type
is
2078 Official
: constant String :=
2079 C
.Strings
.Value
(E
.H_Name
);
2081 Aliases
: constant Chars_Ptr_Array
:=
2082 Chars_Ptr_Pointers
.Value
(E
.H_Aliases
);
2083 -- H_Aliases points to a list of name aliases. The list is terminated by
2086 Addresses
: constant In_Addr_Access_Array
:=
2087 In_Addr_Access_Pointers
.Value
(E
.H_Addr_List
);
2088 -- H_Addr_List points to a list of binary addresses (in network byte
2089 -- order). The list is terminated by a NULL pointer.
2091 -- H_Length is not used because it is currently only set to 4.
2092 -- H_Addrtype is always AF_INET
2094 Result
: Host_Entry_Type
2095 (Aliases_Length
=> Aliases
'Length - 1,
2096 Addresses_Length
=> Addresses
'Length - 1);
2097 -- The last element is a null pointer
2103 Result
.Official
:= To_Name
(Official
);
2105 Source
:= Aliases
'First;
2106 Target
:= Result
.Aliases
'First;
2107 while Target
<= Result
.Aliases_Length
loop
2108 Result
.Aliases
(Target
) :=
2109 To_Name
(C
.Strings
.Value
(Aliases
(Source
)));
2110 Source
:= Source
+ 1;
2111 Target
:= Target
+ 1;
2114 Source
:= Addresses
'First;
2115 Target
:= Result
.Addresses
'First;
2116 while Target
<= Result
.Addresses_Length
loop
2117 To_Inet_Addr
(Addresses
(Source
).all, Result
.Addresses
(Target
));
2118 Source
:= Source
+ 1;
2119 Target
:= Target
+ 1;
2129 function To_In_Addr
(Addr
: Inet_Addr_Type
) return Thin
.In_Addr
is
2131 if Addr
.Family
= Family_Inet
then
2132 return (S_B1
=> C
.unsigned_char
(Addr
.Sin_V4
(1)),
2133 S_B2
=> C
.unsigned_char
(Addr
.Sin_V4
(2)),
2134 S_B3
=> C
.unsigned_char
(Addr
.Sin_V4
(3)),
2135 S_B4
=> C
.unsigned_char
(Addr
.Sin_V4
(4)));
2145 procedure To_Inet_Addr
2147 Result
: out Inet_Addr_Type
) is
2149 Result
.Sin_V4
(1) := Inet_Addr_Comp_Type
(Addr
.S_B1
);
2150 Result
.Sin_V4
(2) := Inet_Addr_Comp_Type
(Addr
.S_B2
);
2151 Result
.Sin_V4
(3) := Inet_Addr_Comp_Type
(Addr
.S_B3
);
2152 Result
.Sin_V4
(4) := Inet_Addr_Comp_Type
(Addr
.S_B4
);
2159 function To_Int
(F
: Request_Flag_Type
) return C
.int
2161 Current
: Request_Flag_Type
:= F
;
2162 Result
: C
.int
:= 0;
2165 for J
in Flags
'Range loop
2166 exit when Current
= 0;
2168 if Current
mod 2 /= 0 then
2169 if Flags
(J
) = -1 then
2170 Raise_Socket_Error
(Constants
.EOPNOTSUPP
);
2173 Result
:= Result
+ Flags
(J
);
2176 Current
:= Current
/ 2;
2186 function To_Name
(N
: String) return Name_Type
is
2188 return Name_Type
'(N'Length, N);
2191 ----------------------
2192 -- To_Service_Entry --
2193 ----------------------
2195 function To_Service_Entry (E : Servent) return Service_Entry_Type is
2198 Official : constant String :=
2199 C.Strings.Value (E.S_Name);
2201 Aliases : constant Chars_Ptr_Array :=
2202 Chars_Ptr_Pointers.Value (E.S_Aliases);
2203 -- S_Aliases points to a list of name aliases. The list is
2204 -- terminated by a NULL pointer.
2206 Protocol : constant String :=
2207 C.Strings.Value (E.S_Proto);
2209 Result : Service_Entry_Type
2210 (Aliases_Length => Aliases'Length - 1);
2211 -- The last element is a null pointer
2217 Result.Official := To_Name (Official);
2219 Source := Aliases'First;
2220 Target := Result.Aliases'First;
2221 while Target <= Result.Aliases_Length loop
2222 Result.Aliases (Target) :=
2223 To_Name (C.Strings.Value (Aliases (Source)));
2224 Source := Source + 1;
2225 Target := Target + 1;
2229 Port_Type (Network_To_Short (C.unsigned_short (E.S_Port)));
2231 Result.Protocol := To_Name (Protocol);
2234 end To_Service_Entry;
2240 function To_String (HN : Name_Type) return String is
2242 return HN.Name (1 .. HN.Length);
2249 function To_Timeval (Val : Timeval_Duration) return Timeval is
2254 -- If zero, set result as zero (otherwise it gets rounded down to -1)
2260 -- Normal case where we do round down
2263 S := time_t (Val - 0.5);
2264 uS := suseconds_t (1_000_000 * (Val - Selector_Duration (S)));
2275 (Stream : in out Datagram_Socket_Stream_Type;
2276 Item : Ada.Streams.Stream_Element_Array)
2278 First : Ada.Streams.Stream_Element_Offset := Item'First;
2279 Index : Ada.Streams.Stream_Element_Offset := First - 1;
2280 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
2286 Item (First .. Max),
2290 -- Exit when all or zero data sent. Zero means that the socket has
2291 -- been closed by peer.
2293 exit when Index < First or else Index = Max;
2298 if Index /= Max then
2308 (Stream : in out Stream_Socket_Stream_Type;
2309 Item : Ada.Streams.Stream_Element_Array)
2311 First : Ada.Streams.Stream_Element_Offset := Item'First;
2312 Index : Ada.Streams.Stream_Element_Offset := First - 1;
2313 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
2317 Send_Socket (Stream.Socket, Item (First .. Max), Index);
2319 -- Exit when all or zero data sent. Zero means that the socket has
2320 -- been closed by peer.
2322 exit when Index < First or else Index = Max;
2327 if Index /= Max then