1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S --
11 -- Copyright (C) 2001 Ada Core Technologies, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
35 with Ada.Streams; use Ada.Streams;
36 with Ada.Exceptions; use Ada.Exceptions;
37 with Ada.Unchecked_Deallocation;
38 with Ada.Unchecked_Conversion;
40 with Interfaces.C.Strings;
42 with GNAT.OS_Lib; use GNAT.OS_Lib;
43 with GNAT.Sockets.Constants;
44 with GNAT.Sockets.Thin; use GNAT.Sockets.Thin;
47 with GNAT.Sockets.Linker_Options;
48 pragma Warnings (Off, GNAT.Sockets.Linker_Options);
49 -- Need to include pragma Linker_Options which is platform dependent.
51 with System; use System;
53 package body GNAT.Sockets is
55 use type C.int, System.Address;
57 Finalized : Boolean := False;
58 Initialized : Boolean := False;
60 -- Correspondance tables
62 Families : constant array (Family_Type) of C.int :=
63 (Family_Inet => Constants.AF_INET,
64 Family_Inet6 => Constants.AF_INET6);
66 Levels : constant array (Level_Type) of C.int :=
67 (Socket_Level => Constants.SOL_SOCKET,
68 IP_Protocol_For_IP_Level => Constants.IPPROTO_IP,
69 IP_Protocol_For_UDP_Level => Constants.IPPROTO_UDP,
70 IP_Protocol_For_TCP_Level => Constants.IPPROTO_TCP);
72 Modes : constant array (Mode_Type) of C.int :=
73 (Socket_Stream => Constants.SOCK_STREAM,
74 Socket_Datagram => Constants.SOCK_DGRAM);
76 Shutmodes : constant array (Shutmode_Type) of C.int :=
77 (Shut_Read => Constants.SHUT_RD,
78 Shut_Write => Constants.SHUT_WR,
79 Shut_Read_Write => Constants.SHUT_RDWR);
81 Requests : constant array (Request_Name) of C.int :=
82 (Non_Blocking_IO => Constants.FIONBIO,
83 N_Bytes_To_Read => Constants.FIONREAD);
85 Options : constant array (Option_Name) of C.int :=
86 (Keep_Alive => Constants.SO_KEEPALIVE,
87 Reuse_Address => Constants.SO_REUSEADDR,
88 Broadcast => Constants.SO_BROADCAST,
89 Send_Buffer => Constants.SO_SNDBUF,
90 Receive_Buffer => Constants.SO_RCVBUF,
91 Linger => Constants.SO_LINGER,
92 Error => Constants.SO_ERROR,
93 No_Delay => Constants.TCP_NODELAY,
94 Add_Membership => Constants.IP_ADD_MEMBERSHIP,
95 Drop_Membership => Constants.IP_DROP_MEMBERSHIP,
96 Multicast_TTL => Constants.IP_MULTICAST_TTL,
97 Multicast_Loop => Constants.IP_MULTICAST_LOOP);
99 Socket_Error_Id : constant Exception_Id := Socket_Error'Identity;
100 Host_Error_Id : constant Exception_Id := Host_Error'Identity;
102 Hex_To_Char : constant String (1 .. 16) := "0123456789ABCDEF";
103 -- Use to print in hexadecimal format
105 function To_In_Addr is new Ada.Unchecked_Conversion (C.int, In_Addr);
106 function To_Int is new Ada.Unchecked_Conversion (In_Addr, C.int);
108 -----------------------
109 -- Local subprograms --
110 -----------------------
112 function Resolve_Error
113 (Error_Value : Integer;
114 From_Errno : Boolean := True)
116 -- Associate an enumeration value (error_type) to en error value
117 -- (errno). From_Errno prevents from mixing h_errno with errno.
119 function To_Host_Name (N : String) return Host_Name_Type;
120 function To_String (HN : Host_Name_Type) return String;
121 -- Conversion functions
123 function Port_To_Network
124 (Port : C.unsigned_short)
125 return C.unsigned_short;
126 pragma Inline (Port_To_Network);
127 -- Convert a port number into a network port number
129 function Network_To_Port
130 (Net_Port : C.unsigned_short)
131 return C.unsigned_short
132 renames Port_To_Network;
133 -- Symetric operation
136 (Val : Inet_Addr_VN_Type;
137 Hex : Boolean := False)
139 -- Output an array of inet address components either in
140 -- hexadecimal or in decimal mode.
142 function To_In_Addr (Addr : Inet_Addr_Type) return Thin.In_Addr;
143 function To_Inet_Addr (Addr : In_Addr) return Inet_Addr_Type;
144 -- Conversion functions
146 function To_Host_Entry (Host : Hostent) return Host_Entry_Type;
147 -- Conversion function
149 function To_Timeval (Val : Duration) return Timeval;
150 -- Separate Val in seconds and microseconds
152 procedure Raise_Socket_Error (Error : Integer);
153 -- Raise Socket_Error with an exception message describing
156 procedure Raise_Host_Error (Error : Integer);
157 -- Raise Host_Error exception with message describing error code
158 -- (note hstrerror seems to be obsolete).
160 -- Types needed for Socket_Set_Type
162 type Socket_Set_Record is new Fd_Set;
165 new Ada.Unchecked_Deallocation (Socket_Set_Record, Socket_Set_Type);
167 -- Types needed for Datagram_Socket_Stream_Type
169 type Datagram_Socket_Stream_Type is new Root_Stream_Type with
171 Socket : Socket_Type;
173 From : Sock_Addr_Type;
176 type Datagram_Socket_Stream_Access is
177 access all Datagram_Socket_Stream_Type;
180 (Stream : in out Datagram_Socket_Stream_Type;
181 Item : out Ada.Streams.Stream_Element_Array;
182 Last : out Ada.Streams.Stream_Element_Offset);
185 (Stream : in out Datagram_Socket_Stream_Type;
186 Item : Ada.Streams.Stream_Element_Array);
188 -- Types needed for Stream_Socket_Stream_Type
190 type Stream_Socket_Stream_Type is new Root_Stream_Type with
192 Socket : Socket_Type;
195 type Stream_Socket_Stream_Access is
196 access all Stream_Socket_Stream_Type;
199 (Stream : in out Stream_Socket_Stream_Type;
200 Item : out Ada.Streams.Stream_Element_Array;
201 Last : out Ada.Streams.Stream_Element_Offset);
204 (Stream : in out Stream_Socket_Stream_Type;
205 Item : Ada.Streams.Stream_Element_Array);
211 procedure Abort_Selector (Selector : Selector_Type) is
213 -- Send an empty array to unblock C select system call
215 if Selector.In_Progress then
220 Res := C_Write (C.int (Selector.W_Sig_Socket), Buf'Address, 0);
229 procedure Accept_Socket
230 (Server : Socket_Type;
231 Socket : out Socket_Type;
232 Address : out Sock_Addr_Type)
235 Sin : aliased Sockaddr_In;
236 Len : aliased C.int := Sin'Size / 8;
239 Res := C_Accept (C.int (Server), Sin'Address, Len'Access);
240 if Res = Failure then
241 Raise_Socket_Error (Socket_Errno);
244 Socket := Socket_Type (Res);
246 Address.Addr := To_Inet_Addr (Sin.Sin_Addr);
247 Address.Port := Port_Type (Network_To_Port (Sin.Sin_Port));
255 (E : Host_Entry_Type;
257 return Inet_Addr_Type
260 return E.Addresses (N);
263 ----------------------
264 -- Addresses_Length --
265 ----------------------
267 function Addresses_Length (E : Host_Entry_Type) return Natural is
269 return E.Addresses_Length;
270 end Addresses_Length;
277 (E : Host_Entry_Type;
282 return To_String (E.Aliases (N));
289 function Aliases_Length (E : Host_Entry_Type) return Natural is
291 return E.Aliases_Length;
298 procedure Bind_Socket
299 (Socket : Socket_Type;
300 Address : Sock_Addr_Type)
303 Sin : aliased Sockaddr_In;
304 Len : aliased C.int := Sin'Size / 8;
307 if Address.Family = Family_Inet6 then
311 Sin.Sin_Family := C.unsigned_short (Families (Address.Family));
312 Sin.Sin_Port := Port_To_Network (C.unsigned_short (Address.Port));
314 Res := C_Bind (C.int (Socket), Sin'Address, Len);
316 if Res = Failure then
317 Raise_Socket_Error (Socket_Errno);
325 procedure Check_Selector
326 (Selector : in out Selector_Type;
327 R_Socket_Set : in out Socket_Set_Type;
328 W_Socket_Set : in out Socket_Set_Type;
329 Status : out Selector_Status;
330 Timeout : Duration := Forever)
334 RSet : aliased Fd_Set;
335 WSet : aliased Fd_Set;
336 TVal : aliased Timeval;
337 TPtr : Timeval_Access;
342 -- No timeout or Forever is indicated by a null timeval pointer.
344 if Timeout = Forever then
347 TVal := To_Timeval (Timeout);
348 TPtr := TVal'Unchecked_Access;
351 -- Copy R_Socket_Set in RSet and add read signalling socket.
353 if R_Socket_Set = null then
356 RSet := Fd_Set (R_Socket_Set.all);
359 Set (RSet, C.int (Selector.R_Sig_Socket));
360 Len := Max (RSet) + 1;
362 -- Copy W_Socket_Set in WSet.
364 if W_Socket_Set = null then
367 WSet := Fd_Set (W_Socket_Set.all);
369 Len := C.int'Max (Max (RSet) + 1, Len);
371 Selector.In_Progress := True;
375 RSet'Unchecked_Access,
376 WSet'Unchecked_Access,
378 Selector.In_Progress := False;
380 -- If Select was resumed because of read signalling socket,
381 -- read this data and remove socket from set.
383 if Is_Set (RSet, C.int (Selector.R_Sig_Socket)) then
384 Clear (RSet, C.int (Selector.R_Sig_Socket));
389 Res := C_Read (C.int (Selector.R_Sig_Socket), Buf'Address, 0);
392 -- Select was resumed because of read signalling socket, but
393 -- the call is said aborted only when there is no other read
397 and then Is_Empty (WSet)
406 if R_Socket_Set /= null then
407 R_Socket_Set.all := Socket_Set_Record (RSet);
410 if W_Socket_Set /= null then
411 W_Socket_Set.all := Socket_Set_Record (WSet);
420 (Item : in out Socket_Set_Type;
421 Socket : Socket_Type)
425 Item := new Socket_Set_Record;
426 Empty (Fd_Set (Item.all));
429 Clear (Fd_Set (Item.all), C.int (Socket));
436 procedure Close_Selector (Selector : in out Selector_Type) is
439 Close_Socket (Selector.R_Sig_Socket);
440 exception when Socket_Error =>
445 Close_Socket (Selector.W_Sig_Socket);
446 exception when Socket_Error =>
455 procedure Close_Socket (Socket : Socket_Type) is
459 Res := C_Close (C.int (Socket));
461 if Res = Failure then
462 Raise_Socket_Error (Socket_Errno);
470 procedure Connect_Socket
471 (Socket : Socket_Type;
472 Server : in out Sock_Addr_Type)
475 Sin : aliased Sockaddr_In;
476 Len : aliased C.int := Sin'Size / 8;
479 if Server.Family = Family_Inet6 then
483 Sin.Sin_Family := C.unsigned_short (Families (Server.Family));
484 Sin.Sin_Addr := To_In_Addr (Server.Addr);
485 Sin.Sin_Port := Port_To_Network (C.unsigned_short (Server.Port));
487 Res := C_Connect (C.int (Socket), Sin'Address, Len);
489 if Res = Failure then
490 Raise_Socket_Error (Socket_Errno);
498 procedure Control_Socket
499 (Socket : Socket_Type;
500 Request : in out Request_Type)
507 when Non_Blocking_IO =>
508 Arg := C.int (Boolean'Pos (Request.Enabled));
510 when N_Bytes_To_Read =>
517 Requests (Request.Name),
518 Arg'Unchecked_Access);
520 if Res = Failure then
521 Raise_Socket_Error (Socket_Errno);
525 when Non_Blocking_IO =>
528 when N_Bytes_To_Read =>
529 Request.Size := Natural (Arg);
534 ---------------------
535 -- Create_Selector --
536 ---------------------
538 procedure Create_Selector (Selector : out Selector_Type) is
543 Sin : aliased Sockaddr_In;
544 Len : aliased C.int := Sin'Size / 8;
548 -- We open two signalling sockets. One socket to send a signal
549 -- to a another socket that always included in a C_Select
550 -- socket set. When received, it resumes the task suspended in
553 -- Create a listening socket
555 S0 := C_Socket (Constants.AF_INET, Constants.SOCK_STREAM, 0);
557 Raise_Socket_Error (Socket_Errno);
560 -- Sin is already correctly initialized. Bind the socket to any
563 Res := C_Bind (S0, Sin'Address, Len);
564 if Res = Failure then
567 Raise_Socket_Error (Err);
570 -- Get the port used by the socket
572 Res := C_Getsockname (S0, Sin'Address, Len'Access);
573 if Res = Failure then
576 Raise_Socket_Error (Err);
579 Res := C_Listen (S0, 2);
580 if Res = Failure then
583 Raise_Socket_Error (Err);
586 S1 := C_Socket (Constants.AF_INET, Constants.SOCK_STREAM, 0);
590 Raise_Socket_Error (Err);
593 -- Use INADDR_LOOPBACK
595 Sin.Sin_Addr.S_B1 := 127;
596 Sin.Sin_Addr.S_B2 := 0;
597 Sin.Sin_Addr.S_B3 := 0;
598 Sin.Sin_Addr.S_B4 := 1;
600 -- Do a connect and accept the connection
602 Res := C_Connect (S1, Sin'Address, Len);
603 if Res = Failure then
607 Raise_Socket_Error (Err);
610 S2 := C_Accept (S0, Sin'Address, Len'Access);
615 Raise_Socket_Error (Err);
619 if Res = Failure then
620 Raise_Socket_Error (Socket_Errno);
623 Selector.R_Sig_Socket := Socket_Type (S1);
624 Selector.W_Sig_Socket := Socket_Type (S2);
631 procedure Create_Socket
632 (Socket : out Socket_Type;
633 Family : Family_Type := Family_Inet;
634 Mode : Mode_Type := Socket_Stream)
639 Res := C_Socket (Families (Family), Modes (Mode), 0);
641 if Res = Failure then
642 Raise_Socket_Error (Socket_Errno);
645 Socket := Socket_Type (Res);
652 procedure Empty (Item : in out Socket_Set_Type) is
663 procedure Finalize is
677 function Get_Address (Stream : Stream_Access) return Sock_Addr_Type is
679 if Stream = null then
682 elsif Stream.all in Datagram_Socket_Stream_Type then
683 return Datagram_Socket_Stream_Type (Stream.all).From;
686 return Get_Peer_Name (Stream_Socket_Stream_Type (Stream.all).Socket);
690 -------------------------
691 -- Get_Host_By_Address --
692 -------------------------
694 function Get_Host_By_Address
695 (Address : Inet_Addr_Type;
696 Family : Family_Type := Family_Inet)
697 return Host_Entry_Type
699 HA : aliased In_Addr := To_In_Addr (Address);
700 Res : Hostent_Access;
704 -- This C function is not always thread-safe. Protect against
705 -- concurrent access.
708 Res := C_Gethostbyaddr (HA'Address, HA'Size / 8, Constants.AF_INET);
713 Raise_Host_Error (Err);
716 -- Translate from the C format to the API format
719 HE : Host_Entry_Type := To_Host_Entry (Res.all);
725 end Get_Host_By_Address;
727 ----------------------
728 -- Get_Host_By_Name --
729 ----------------------
731 function Get_Host_By_Name
733 return Host_Entry_Type
735 HN : C.char_array := C.To_C (Name);
736 Res : Hostent_Access;
740 -- This C function is not always thread-safe. Protect against
741 -- concurrent access.
744 Res := C_Gethostbyname (HN);
749 Raise_Host_Error (Err);
752 -- Translate from the C format to the API format
755 HE : Host_Entry_Type := To_Host_Entry (Res.all);
761 end Get_Host_By_Name;
767 function Get_Peer_Name
768 (Socket : Socket_Type)
769 return Sock_Addr_Type
771 Sin : aliased Sockaddr_In;
772 Len : aliased C.int := Sin'Size / 8;
773 Res : Sock_Addr_Type (Family_Inet);
776 if C_Getpeername (C.int (Socket), Sin'Address, Len'Access) = Failure then
777 Raise_Socket_Error (Socket_Errno);
780 Res.Addr := To_Inet_Addr (Sin.Sin_Addr);
781 Res.Port := Port_Type (Network_To_Port (Sin.Sin_Port));
786 ---------------------
787 -- Get_Socket_Name --
788 ---------------------
790 function Get_Socket_Name
791 (Socket : Socket_Type)
792 return Sock_Addr_Type
794 Sin : aliased Sockaddr_In;
795 Len : aliased C.int := Sin'Size / 8;
796 Res : Sock_Addr_Type (Family_Inet);
799 if C_Getsockname (C.int (Socket), Sin'Address, Len'Access) = Failure then
800 Raise_Socket_Error (Socket_Errno);
803 Res.Addr := To_Inet_Addr (Sin.Sin_Addr);
804 Res.Port := Port_Type (Network_To_Port (Sin.Sin_Port));
809 -----------------------
810 -- Get_Socket_Option --
811 -----------------------
813 function Get_Socket_Option
814 (Socket : Socket_Type;
815 Level : Level_Type := Socket_Level;
819 use type C.unsigned_char;
821 V8 : aliased Two_Int;
823 V1 : aliased C.unsigned_char;
825 Add : System.Address;
827 Opt : Option_Type (Name);
831 when Multicast_Loop |
858 Add, Len'Unchecked_Access);
860 if Res = Failure then
861 Raise_Socket_Error (Socket_Errno);
869 Opt.Enabled := (V4 /= 0);
872 Opt.Enabled := (V8 (V8'First) /= 0);
873 Opt.Seconds := Natural (V8 (V8'Last));
877 Opt.Size := Natural (V4);
880 Opt.Error := Resolve_Error (Integer (V4));
882 when Add_Membership |
884 Opt.Multiaddr := To_Inet_Addr (To_In_Addr (V8 (V8'First)));
885 Opt.Interface := To_Inet_Addr (To_In_Addr (V8 (V8'Last)));
887 when Multicast_TTL =>
888 Opt.Time_To_Live := Integer (V1);
890 when Multicast_Loop =>
891 Opt.Enabled := (V1 /= 0);
896 end Get_Socket_Option;
902 function Host_Name return String is
903 Name : aliased C.char_array (1 .. 64);
907 Res := C_Gethostname (Name'Address, Name'Length);
909 if Res = Failure then
910 Raise_Socket_Error (Socket_Errno);
913 return C.To_Ada (Name);
921 (Val : Inet_Addr_VN_Type;
922 Hex : Boolean := False)
925 -- The largest Inet_Addr_Comp_Type image occurs with IPv4. It
926 -- has at most a length of 3 plus one '.' character.
928 Buffer : String (1 .. 4 * Val'Length);
929 Length : Natural := 1;
930 Separator : Character;
932 procedure Img10 (V : Inet_Addr_Comp_Type);
933 -- Append to Buffer image of V in decimal format
935 procedure Img16 (V : Inet_Addr_Comp_Type);
936 -- Append to Buffer image of V in hexadecimal format
938 procedure Img10 (V : Inet_Addr_Comp_Type) is
939 Img : constant String := V'Img;
940 Len : Natural := Img'Length - 1;
943 Buffer (Length .. Length + Len - 1) := Img (2 .. Img'Last);
944 Length := Length + Len;
947 procedure Img16 (V : Inet_Addr_Comp_Type) is
949 Buffer (Length) := Hex_To_Char (Natural (V / 16) + 1);
950 Buffer (Length + 1) := Hex_To_Char (Natural (V mod 16) + 1);
951 Length := Length + 2;
954 -- Start of processing for Image
963 for J in Val'Range loop
970 if J /= Val'Last then
971 Buffer (Length) := Separator;
972 Length := Length + 1;
976 return Buffer (1 .. Length - 1);
983 function Image (Value : Inet_Addr_Type) return String is
985 if Value.Family = Family_Inet then
986 return Image (Inet_Addr_VN_Type (Value.Sin_V4), Hex => False);
988 return Image (Inet_Addr_VN_Type (Value.Sin_V6), Hex => True);
996 function Image (Value : Sock_Addr_Type) return String is
997 Port : constant String := Value.Port'Img;
1000 return Image (Value.Addr) & ':' & Port (2 .. Port'Last);
1007 function Image (Socket : Socket_Type) return String is
1016 function Inet_Addr (Image : String) return Inet_Addr_Type is
1017 use Interfaces.C.Strings;
1019 Img : chars_ptr := New_String (Image);
1024 Res := C_Inet_Addr (Img);
1028 if Res = Failure then
1029 Raise_Socket_Error (Err);
1032 return To_Inet_Addr (To_In_Addr (Res));
1039 procedure Initialize (Process_Blocking_IO : Boolean := False) is
1041 if not Initialized then
1042 Initialized := True;
1043 Thin.Initialize (Process_Blocking_IO);
1051 function Is_Empty (Item : Socket_Set_Type) return Boolean is
1053 return Item = null or else Is_Empty (Fd_Set (Item.all));
1061 (Item : Socket_Set_Type;
1062 Socket : Socket_Type) return Boolean
1066 and then Is_Set (Fd_Set (Item.all), C.int (Socket));
1073 procedure Listen_Socket
1074 (Socket : Socket_Type;
1075 Length : Positive := 15)
1080 Res := C_Listen (C.int (Socket), C.int (Length));
1081 if Res = Failure then
1082 Raise_Socket_Error (Socket_Errno);
1090 function Official_Name (E : Host_Entry_Type) return String is
1092 return To_String (E.Official);
1095 ---------------------
1096 -- Port_To_Network --
1097 ---------------------
1099 function Port_To_Network
1100 (Port : C.unsigned_short)
1101 return C.unsigned_short
1103 use type C.unsigned_short;
1105 if Default_Bit_Order = High_Order_First then
1107 -- No conversion needed. On these platforms, htons() defaults
1108 -- to a null procedure.
1113 -- We need to swap the high and low byte on this short to make
1114 -- the port number network compliant.
1116 return (Port / 256) + (Port mod 256) * 256;
1118 end Port_To_Network;
1120 ----------------------
1121 -- Raise_Host_Error --
1122 ----------------------
1124 procedure Raise_Host_Error (Error : Integer) is
1126 function Error_Message return String;
1127 -- We do not use a C function like strerror because hstrerror
1128 -- that would correspond seems to be obsolete. Return
1129 -- appropriate string for error value.
1131 function Error_Message return String is
1134 when Constants.HOST_NOT_FOUND => return "Host not found";
1135 when Constants.TRY_AGAIN => return "Try again";
1136 when Constants.NO_RECOVERY => return "No recovery";
1137 when Constants.NO_ADDRESS => return "No address";
1138 when others => return "Unknown error";
1142 -- Start of processing for Raise_Host_Error
1145 Ada.Exceptions.Raise_Exception (Host_Error'Identity, Error_Message);
1146 end Raise_Host_Error;
1148 ------------------------
1149 -- Raise_Socket_Error --
1150 ------------------------
1152 procedure Raise_Socket_Error (Error : Integer) is
1153 use type C.Strings.chars_ptr;
1155 function Image (E : Integer) return String;
1156 function Image (E : Integer) return String is
1157 Msg : String := E'Img & "] ";
1159 Msg (Msg'First) := '[';
1164 Ada.Exceptions.Raise_Exception
1165 (Socket_Error'Identity, Image (Error) & Socket_Error_Message (Error));
1166 end Raise_Socket_Error;
1173 (Stream : in out Datagram_Socket_Stream_Type;
1174 Item : out Ada.Streams.Stream_Element_Array;
1175 Last : out Ada.Streams.Stream_Element_Offset)
1177 First : Ada.Streams.Stream_Element_Offset := Item'First;
1178 Index : Ada.Streams.Stream_Element_Offset := First - 1;
1179 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
1185 Item (First .. Max),
1191 -- Exit when all or zero data received. Zero means that
1192 -- the socket peer is closed.
1194 exit when Index < First or else Index = Max;
1205 (Stream : in out Stream_Socket_Stream_Type;
1206 Item : out Ada.Streams.Stream_Element_Array;
1207 Last : out Ada.Streams.Stream_Element_Offset)
1209 First : Ada.Streams.Stream_Element_Offset := Item'First;
1210 Index : Ada.Streams.Stream_Element_Offset := First - 1;
1211 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
1215 Receive_Socket (Stream.Socket, Item (First .. Max), Index);
1218 -- Exit when all or zero data received. Zero means that
1219 -- the socket peer is closed.
1221 exit when Index < First or else Index = Max;
1231 function Resolve_Error
1232 (Error_Value : Integer;
1233 From_Errno : Boolean := True)
1236 use GNAT.Sockets.Constants;
1239 if not From_Errno then
1241 when HOST_NOT_FOUND => return Unknown_Host;
1242 when TRY_AGAIN => return Host_Name_Lookup_Failure;
1243 when NO_RECOVERY => return No_Address_Associated_With_Name;
1244 when NO_ADDRESS => return Unknown_Server_Error;
1245 when others => return Cannot_Resolve_Error;
1249 when EACCES => return Permission_Denied;
1250 when EADDRINUSE => return Address_Already_In_Use;
1251 when EADDRNOTAVAIL => return Cannot_Assign_Requested_Address;
1252 when EAFNOSUPPORT =>
1253 return Address_Family_Not_Supported_By_Protocol;
1254 when EALREADY => return Operation_Already_In_Progress;
1255 when EBADF => return Bad_File_Descriptor;
1256 when ECONNREFUSED => return Connection_Refused;
1257 when EFAULT => return Bad_Address;
1258 when EINPROGRESS => return Operation_Now_In_Progress;
1259 when EINTR => return Interrupted_System_Call;
1260 when EINVAL => return Invalid_Argument;
1261 when EIO => return Input_Output_Error;
1262 when EISCONN => return Transport_Endpoint_Already_Connected;
1263 when EMSGSIZE => return Message_Too_Long;
1264 when ENETUNREACH => return Network_Is_Unreachable;
1265 when ENOBUFS => return No_Buffer_Space_Available;
1266 when ENOPROTOOPT => return Protocol_Not_Available;
1267 when ENOTCONN => return Transport_Endpoint_Not_Connected;
1268 when EOPNOTSUPP => return Operation_Not_Supported;
1269 when EPROTONOSUPPORT => return Protocol_Not_Supported;
1270 when ESOCKTNOSUPPORT => return Socket_Type_Not_Supported;
1271 when ETIMEDOUT => return Connection_Timed_Out;
1272 when EWOULDBLOCK => return Resource_Temporarily_Unavailable;
1273 when others => return Cannot_Resolve_Error;
1277 -----------------------
1278 -- Resolve_Exception --
1279 -----------------------
1281 function Resolve_Exception
1282 (Occurrence : Exception_Occurrence)
1285 Id : Exception_Id := Exception_Identity (Occurrence);
1286 Msg : constant String := Exception_Message (Occurrence);
1287 First : Natural := Msg'First;
1292 while First <= Msg'Last
1293 and then Msg (First) not in '0' .. '9'
1298 if First > Msg'Last then
1299 return Cannot_Resolve_Error;
1304 while Last < Msg'Last
1305 and then Msg (Last + 1) in '0' .. '9'
1310 Val := Integer'Value (Msg (First .. Last));
1312 if Id = Socket_Error_Id then
1313 return Resolve_Error (Val);
1315 elsif Id = Host_Error_Id then
1316 return Resolve_Error (Val, False);
1319 return Cannot_Resolve_Error;
1321 end Resolve_Exception;
1323 --------------------
1324 -- Receive_Socket --
1325 --------------------
1327 procedure Receive_Socket
1328 (Socket : Socket_Type;
1329 Item : out Ada.Streams.Stream_Element_Array;
1330 Last : out Ada.Streams.Stream_Element_Offset)
1332 use type Ada.Streams.Stream_Element_Offset;
1339 Item (Item'First)'Address,
1342 if Res = Failure then
1343 Raise_Socket_Error (Socket_Errno);
1346 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1349 --------------------
1350 -- Receive_Socket --
1351 --------------------
1353 procedure Receive_Socket
1354 (Socket : Socket_Type;
1355 Item : out Ada.Streams.Stream_Element_Array;
1356 Last : out Ada.Streams.Stream_Element_Offset;
1357 From : out Sock_Addr_Type)
1359 use type Ada.Streams.Stream_Element_Offset;
1362 Sin : aliased Sockaddr_In;
1363 Len : aliased C.int := Sin'Size / 8;
1368 Item (Item'First)'Address,
1370 Sin'Unchecked_Access,
1371 Len'Unchecked_Access);
1373 if Res = Failure then
1374 Raise_Socket_Error (Socket_Errno);
1377 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1379 From.Addr := To_Inet_Addr (Sin.Sin_Addr);
1380 From.Port := Port_Type (Network_To_Port (Sin.Sin_Port));
1387 procedure Send_Socket
1388 (Socket : Socket_Type;
1389 Item : Ada.Streams.Stream_Element_Array;
1390 Last : out Ada.Streams.Stream_Element_Offset)
1392 use type Ada.Streams.Stream_Element_Offset;
1399 Item (Item'First)'Address,
1402 if Res = Failure then
1403 Raise_Socket_Error (Socket_Errno);
1406 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1413 procedure Send_Socket
1414 (Socket : Socket_Type;
1415 Item : Ada.Streams.Stream_Element_Array;
1416 Last : out Ada.Streams.Stream_Element_Offset;
1417 To : Sock_Addr_Type)
1419 use type Ada.Streams.Stream_Element_Offset;
1422 Sin : aliased Sockaddr_In;
1423 Len : aliased C.int := Sin'Size / 8;
1426 Sin.Sin_Family := C.unsigned_short (Families (To.Family));
1427 Sin.Sin_Addr := To_In_Addr (To.Addr);
1428 Sin.Sin_Port := Port_To_Network (C.unsigned_short (To.Port));
1432 Item (Item'First)'Address,
1434 Sin'Unchecked_Access,
1437 if Res = Failure then
1438 Raise_Socket_Error (Socket_Errno);
1441 Last := Item'First + Ada.Streams.Stream_Element_Offset (Res - 1);
1448 procedure Set (Item : in out Socket_Set_Type; Socket : Socket_Type) is
1451 Item := new Socket_Set_Record'(Socket_Set_Record (Null_Fd_Set));
1454 Set (Fd_Set (Item.all), C.int (Socket));
1457 -----------------------
1458 -- Set_Socket_Option --
1459 -----------------------
1461 procedure Set_Socket_Option
1462 (Socket : Socket_Type;
1463 Level : Level_Type := Socket_Level;
1464 Option : Option_Type)
1466 V8 : aliased Two_Int;
1468 V1 : aliased C.unsigned_char;
1469 Len : aliased C.int;
1470 Add : System.Address := Null_Address;
1479 V4 := C.int (Boolean'Pos (Option.Enabled));
1484 V8 (V8'First) := C.int (Boolean'Pos (Option.Enabled));
1485 V8 (V8'Last) := C.int (Option.Seconds);
1491 V4 := C.int (Option.Size);
1496 V4 := C.int (Boolean'Pos (True));
1500 when Add_Membership |
1502 V8 (V8'First) := To_Int (To_In_Addr (Option.Multiaddr));
1503 V8 (V8'Last) := To_Int (To_In_Addr (Option.Interface));
1507 when Multicast_TTL =>
1508 V1 := C.unsigned_char (Option.Time_To_Live);
1512 when Multicast_Loop =>
1513 V1 := C.unsigned_char (Boolean'Pos (Option.Enabled));
1522 Options (Option.Name),
1525 if Res = Failure then
1526 Raise_Socket_Error (Socket_Errno);
1528 end Set_Socket_Option;
1530 ---------------------
1531 -- Shutdown_Socket --
1532 ---------------------
1534 procedure Shutdown_Socket
1535 (Socket : Socket_Type;
1536 How : Shutmode_Type := Shut_Read_Write)
1541 Res := C_Shutdown (C.int (Socket), Shutmodes (How));
1542 if Res = Failure then
1543 Raise_Socket_Error (Socket_Errno);
1545 end Shutdown_Socket;
1552 (Socket : Socket_Type;
1553 Send_To : Sock_Addr_Type)
1554 return Stream_Access
1556 S : Datagram_Socket_Stream_Access;
1559 S := new Datagram_Socket_Stream_Type;
1562 S.From := Get_Socket_Name (Socket);
1563 return Stream_Access (S);
1571 (Socket : Socket_Type)
1572 return Stream_Access
1574 S : Stream_Socket_Stream_Access;
1577 S := new Stream_Socket_Stream_Type;
1579 return Stream_Access (S);
1586 function To_C (Socket : Socket_Type) return Integer is
1588 return Integer (Socket);
1595 function To_Host_Entry
1597 return Host_Entry_Type
1601 Official : constant String :=
1602 C.Strings.Value (Host.H_Name);
1604 Aliases : constant Chars_Ptr_Array :=
1605 Chars_Ptr_Pointers.Value (Host.H_Aliases);
1606 -- H_Aliases points to a list of name aliases. The list is
1607 -- terminated by a NULL pointer.
1609 Addresses : constant In_Addr_Access_Array :=
1610 In_Addr_Access_Pointers.Value (Host.H_Addr_List);
1611 -- H_Addr_List points to a list of binary addresses (in network
1612 -- byte order). The list is terminated by a NULL pointer.
1614 -- H_Length is not used because it is currently only set to 4.
1615 -- H_Addrtype is always AF_INET
1617 Result : Host_Entry_Type
1618 (Aliases_Length => Aliases'Length - 1,
1619 Addresses_Length => Addresses'Length - 1);
1620 -- The last element is a null pointer.
1626 Result.Official := To_Host_Name (Official);
1628 Source := Aliases'First;
1629 Target := Result.Aliases'First;
1630 while Target <= Result.Aliases_Length loop
1631 Result.Aliases (Target) :=
1632 To_Host_Name (C.Strings.Value (Aliases (Source)));
1633 Source := Source + 1;
1634 Target := Target + 1;
1637 Source := Addresses'First;
1638 Target := Result.Addresses'First;
1639 while Target <= Result.Addresses_Length loop
1640 Result.Addresses (Target) :=
1641 To_Inet_Addr (Addresses (Source).all);
1642 Source := Source + 1;
1643 Target := Target + 1;
1653 function To_Host_Name (N : String) return Host_Name_Type is
1655 return (N'Length, N);
1662 function To_In_Addr (Addr : Inet_Addr_Type) return Thin.In_Addr is
1664 if Addr.Family = Family_Inet then
1665 return (S_B1 => C.unsigned_char (Addr.Sin_V4 (1)),
1666 S_B2 => C.unsigned_char (Addr.Sin_V4 (2)),
1667 S_B3 => C.unsigned_char (Addr.Sin_V4 (3)),
1668 S_B4 => C.unsigned_char (Addr.Sin_V4 (4)));
1678 function To_Inet_Addr
1680 return Inet_Addr_Type
1682 Result : Inet_Addr_Type;
1685 Result.Sin_V4 (1) := Inet_Addr_Comp_Type (Addr.S_B1);
1686 Result.Sin_V4 (2) := Inet_Addr_Comp_Type (Addr.S_B2);
1687 Result.Sin_V4 (3) := Inet_Addr_Comp_Type (Addr.S_B3);
1688 Result.Sin_V4 (4) := Inet_Addr_Comp_Type (Addr.S_B4);
1697 function To_String (HN : Host_Name_Type) return String is
1699 return HN.Name (1 .. HN.Length);
1706 function To_Timeval (Val : Duration) return Timeval is
1707 S : Timeval_Unit := Timeval_Unit (Val);
1708 MS : Timeval_Unit := Timeval_Unit (1_000_000 * (Val - Duration (S)));
1719 (Stream : in out Datagram_Socket_Stream_Type;
1720 Item : Ada.Streams.Stream_Element_Array)
1722 First : Ada.Streams.Stream_Element_Offset := Item'First;
1723 Index : Ada.Streams.Stream_Element_Offset := First - 1;
1724 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
1730 Item (First .. Max),
1734 -- Exit when all or zero data sent. Zero means that the
1735 -- socket has been closed by peer.
1737 exit when Index < First or else Index = Max;
1742 if Index /= Max then
1752 (Stream : in out Stream_Socket_Stream_Type;
1753 Item : Ada.Streams.Stream_Element_Array)
1755 First : Ada.Streams.Stream_Element_Offset := Item'First;
1756 Index : Ada.Streams.Stream_Element_Offset := First - 1;
1757 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
1761 Send_Socket (Stream.Socket, Item (First .. Max), Index);
1763 -- Exit when all or zero data sent. Zero means that the
1764 -- socket has been closed by peer.
1766 exit when Index < First or else Index = Max;
1771 if Index /= Max then