Commit 2366e7c6 by Pascal Obry Committed by Arnaud Charlet

g-socthi-mingw.adb (C_Inet_Addr): New body used to convert the returned type on Windows.

2005-07-07  Pascal Obry  <obry@adacore.com>

	* g-socthi-mingw.adb (C_Inet_Addr): New body used to convert the
	returned type on Windows.

	* g-socthi-mingw.ads (C_Inet_Addr): Remove pragma Import for this
	routine.

	* g-socket.adb (Inet_Addr): Check for empty Image and raises an
	exception in this case.
	Simplify the code as "Image (Image'Range)" = "Image".

From-SVN: r101691
parent 41f12ed0
...@@ -117,8 +117,8 @@ package body GNAT.Sockets is ...@@ -117,8 +117,8 @@ package body GNAT.Sockets is
function Resolve_Error function Resolve_Error
(Error_Value : Integer; (Error_Value : Integer;
From_Errno : Boolean := True) return Error_Type; From_Errno : Boolean := True) return Error_Type;
-- Associate an enumeration value (error_type) to en error value -- Associate an enumeration value (error_type) to en error value (errno).
-- (errno). From_Errno prevents from mixing h_errno with errno. -- From_Errno prevents from mixing h_errno with errno.
function To_Name (N : String) return Name_Type; function To_Name (N : String) return Name_Type;
function To_String (HN : Name_Type) return String; function To_String (HN : Name_Type) return String;
...@@ -143,11 +143,10 @@ package body GNAT.Sockets is ...@@ -143,11 +143,10 @@ package body GNAT.Sockets is
function Image function Image
(Val : Inet_Addr_VN_Type; (Val : Inet_Addr_VN_Type;
Hex : Boolean := False) return String; Hex : Boolean := False) return String;
-- Output an array of inet address components either in -- Output an array of inet address components in hex or decimal mode
-- hexadecimal or in decimal mode.
function Is_IP_Address (Name : String) return Boolean; function Is_IP_Address (Name : String) return Boolean;
-- Return true when Name is an IP address in standard dot notation. -- Return true when Name is an IP address in standard dot notation
function To_In_Addr (Addr : Inet_Addr_Type) return Thin.In_Addr; function To_In_Addr (Addr : Inet_Addr_Type) return Thin.In_Addr;
function To_Inet_Addr (Addr : In_Addr) return Inet_Addr_Type; function To_Inet_Addr (Addr : In_Addr) return Inet_Addr_Type;
...@@ -163,12 +162,11 @@ package body GNAT.Sockets is ...@@ -163,12 +162,11 @@ package body GNAT.Sockets is
-- Separate Val in seconds and microseconds -- Separate Val in seconds and microseconds
procedure Raise_Socket_Error (Error : Integer); procedure Raise_Socket_Error (Error : Integer);
-- Raise Socket_Error with an exception message describing -- Raise Socket_Error with an exception message describing the error code
-- the error code.
procedure Raise_Host_Error (Error : Integer); procedure Raise_Host_Error (Error : Integer);
-- Raise Host_Error exception with message describing error code -- Raise Host_Error exception with message describing error code (note
-- (note hstrerror seems to be obsolete). -- hstrerror seems to be obsolete).
procedure Narrow (Item : in out Socket_Set_Type); procedure Narrow (Item : in out Socket_Set_Type);
-- Update Last as it may be greater than the real last socket -- Update Last as it may be greater than the real last socket
...@@ -434,8 +432,8 @@ package body GNAT.Sockets is ...@@ -434,8 +432,8 @@ package body GNAT.Sockets is
Raise_Socket_Error (Socket_Errno); Raise_Socket_Error (Socket_Errno);
end if; end if;
-- If Select was resumed because of read signalling socket, -- If Select was resumed because of read signalling socket, read this
-- read this data and remove socket from set. -- data and remove socket from set.
if Is_Set (RSet, Selector.R_Sig_Socket) then if Is_Set (RSet, Selector.R_Sig_Socket) then
Clear (RSet, Selector.R_Sig_Socket); Clear (RSet, Selector.R_Sig_Socket);
...@@ -457,8 +455,7 @@ package body GNAT.Sockets is ...@@ -457,8 +455,7 @@ package body GNAT.Sockets is
Status := Expired; Status := Expired;
end if; end if;
-- Update RSet, WSet and ESet in regard to their new socket -- Update RSet, WSet and ESet in regard to their new socket sets
-- sets.
Narrow (RSet); Narrow (RSet);
Narrow (WSet); Narrow (WSet);
...@@ -499,7 +496,6 @@ package body GNAT.Sockets is ...@@ -499,7 +496,6 @@ package body GNAT.Sockets is
Socket : Socket_Type) Socket : Socket_Type)
is is
Last : aliased C.int := C.int (Item.Last); Last : aliased C.int := C.int (Item.Last);
begin begin
if Item.Last /= No_Socket then if Item.Last /= No_Socket then
Remove_Socket_From_Set (Item.Set, C.int (Socket)); Remove_Socket_From_Set (Item.Set, C.int (Socket));
...@@ -519,7 +515,6 @@ package body GNAT.Sockets is ...@@ -519,7 +515,6 @@ package body GNAT.Sockets is
begin begin
begin begin
Close_Socket (Selector.R_Sig_Socket); Close_Socket (Selector.R_Sig_Socket);
exception exception
when Socket_Error => when Socket_Error =>
null; null;
...@@ -527,7 +522,6 @@ package body GNAT.Sockets is ...@@ -527,7 +522,6 @@ package body GNAT.Sockets is
begin begin
Close_Socket (Selector.W_Sig_Socket); Close_Socket (Selector.W_Sig_Socket);
exception exception
when Socket_Error => when Socket_Error =>
null; null;
...@@ -616,7 +610,6 @@ package body GNAT.Sockets is ...@@ -616,7 +610,6 @@ package body GNAT.Sockets is
when N_Bytes_To_Read => when N_Bytes_To_Read =>
Request.Size := Natural (Arg); Request.Size := Natural (Arg);
end case; end case;
end Control_Socket; end Control_Socket;
...@@ -651,13 +644,14 @@ package body GNAT.Sockets is ...@@ -651,13 +644,14 @@ package body GNAT.Sockets is
begin begin
-- We open two signalling sockets. One of them is used to send data to -- We open two signalling sockets. One of them is used to send data to
-- send data to the other, which is included in a C_Select socket set. -- the other, which is included in a C_Select socket set. The
-- The communication is used to force the call to C_Select to complete, -- communication is used to force the call to C_Select to complete, and
-- and the waiting task to resume its execution. -- the waiting task to resume its execution.
-- Create a listening socket -- Create a listening socket
S0 := C_Socket (Constants.AF_INET, Constants.SOCK_STREAM, 0); S0 := C_Socket (Constants.AF_INET, Constants.SOCK_STREAM, 0);
if S0 = Failure then if S0 = Failure then
Raise_Socket_Error (Socket_Errno); Raise_Socket_Error (Socket_Errno);
end if; end if;
...@@ -671,6 +665,7 @@ package body GNAT.Sockets is ...@@ -671,6 +665,7 @@ package body GNAT.Sockets is
Sin.Sin_Port := 0; Sin.Sin_Port := 0;
Res := C_Bind (S0, Sin'Address, Len); Res := C_Bind (S0, Sin'Address, Len);
if Res = Failure then if Res = Failure then
Err := Socket_Errno; Err := Socket_Errno;
Res := C_Close (S0); Res := C_Close (S0);
...@@ -819,10 +814,8 @@ package body GNAT.Sockets is ...@@ -819,10 +814,8 @@ package body GNAT.Sockets is
begin begin
if Stream = null then if Stream = null then
raise Socket_Error; raise Socket_Error;
elsif Stream.all in Datagram_Socket_Stream_Type then elsif Stream.all in Datagram_Socket_Stream_Type then
return Datagram_Socket_Stream_Type (Stream.all).From; return Datagram_Socket_Stream_Type (Stream.all).From;
else else
return Get_Peer_Name (Stream_Socket_Stream_Type (Stream.all).Socket); return Get_Peer_Name (Stream_Socket_Stream_Type (Stream.all).Socket);
end if; end if;
...@@ -898,7 +891,6 @@ package body GNAT.Sockets is ...@@ -898,7 +891,6 @@ package body GNAT.Sockets is
declare declare
HE : constant Host_Entry_Type := To_Host_Entry (Res.all); HE : constant Host_Entry_Type := To_Host_Entry (Res.all);
begin begin
Task_Lock.Unlock; Task_Lock.Unlock;
return HE; return HE;
...@@ -1154,7 +1146,6 @@ package body GNAT.Sockets is ...@@ -1154,7 +1146,6 @@ package body GNAT.Sockets is
procedure Img10 (V : Inet_Addr_Comp_Type) is procedure Img10 (V : Inet_Addr_Comp_Type) is
Img : constant String := V'Img; Img : constant String := V'Img;
Len : constant Natural := Img'Length - 1; Len : constant Natural := Img'Length - 1;
begin begin
Buffer (Length .. Length + Len - 1) := Img (2 .. Img'Last); Buffer (Length .. Length + Len - 1) := Img (2 .. Img'Last);
Length := Length + Len; Length := Length + Len;
...@@ -1243,8 +1234,14 @@ package body GNAT.Sockets is ...@@ -1243,8 +1234,14 @@ package body GNAT.Sockets is
-- has the same in_addr_t value as Failure, and thus cannot be -- has the same in_addr_t value as Failure, and thus cannot be
-- properly returned by inet_addr(3). -- properly returned by inet_addr(3).
if Image (Image'Range) = "255.255.255.255" then if Image = "255.255.255.255" then
return Broadcast_Inet_Addr; return Broadcast_Inet_Addr;
-- Special case for an empty Image as on some platforms (e.g. Windows)
-- calling Inet_Addr("") will not return an error.
elsif Image = "" then
Raise_Socket_Error (Constants.EINVAL);
end if; end if;
Img := New_String (Image); Img := New_String (Image);
...@@ -1457,8 +1454,8 @@ package body GNAT.Sockets is ...@@ -1457,8 +1454,8 @@ package body GNAT.Sockets is
Last := Index; Last := Index;
-- Exit when all or zero data received. Zero means that -- Exit when all or zero data received. Zero means that the socket
-- the socket peer is closed. -- peer is closed.
exit when Index < First or else Index = Max; exit when Index < First or else Index = Max;
...@@ -1484,8 +1481,8 @@ package body GNAT.Sockets is ...@@ -1484,8 +1481,8 @@ package body GNAT.Sockets is
Receive_Socket (Stream.Socket, Item (First .. Max), Index); Receive_Socket (Stream.Socket, Item (First .. Max), Index);
Last := Index; Last := Index;
-- Exit when all or zero data received. Zero means that -- Exit when all or zero data received. Zero means that the socket
-- the socket peer is closed. -- peer is closed.
exit when Index < First or else Index = Max; exit when Index < First or else Index = Max;
...@@ -1964,7 +1961,6 @@ package body GNAT.Sockets is ...@@ -1964,7 +1961,6 @@ package body GNAT.Sockets is
function Stream (Socket : Socket_Type) return Stream_Access is function Stream (Socket : Socket_Type) return Stream_Access is
S : Stream_Socket_Stream_Access; S : Stream_Socket_Stream_Access;
begin begin
S := new Stream_Socket_Stream_Type; S := new Stream_Socket_Stream_Type;
S.Socket := Socket; S.Socket := Socket;
...@@ -1992,13 +1988,13 @@ package body GNAT.Sockets is ...@@ -1992,13 +1988,13 @@ package body GNAT.Sockets is
Aliases : constant Chars_Ptr_Array := Aliases : constant Chars_Ptr_Array :=
Chars_Ptr_Pointers.Value (E.H_Aliases); Chars_Ptr_Pointers.Value (E.H_Aliases);
-- H_Aliases points to a list of name aliases. The list is -- H_Aliases points to a list of name aliases. The list is terminated by
-- terminated by a NULL pointer. -- a NULL pointer.
Addresses : constant In_Addr_Access_Array := Addresses : constant In_Addr_Access_Array :=
In_Addr_Access_Pointers.Value (E.H_Addr_List); In_Addr_Access_Pointers.Value (E.H_Addr_List);
-- H_Addr_List points to a list of binary addresses (in network -- H_Addr_List points to a list of binary addresses (in network byte
-- byte order). The list is terminated by a NULL pointer. -- order). The list is terminated by a NULL pointer.
-- --
-- H_Length is not used because it is currently only set to 4. -- H_Length is not used because it is currently only set to 4.
-- H_Addrtype is always AF_INET -- H_Addrtype is always AF_INET
...@@ -2201,8 +2197,8 @@ package body GNAT.Sockets is ...@@ -2201,8 +2197,8 @@ package body GNAT.Sockets is
Index, Index,
Stream.To); Stream.To);
-- Exit when all or zero data sent. Zero means that the -- Exit when all or zero data sent. Zero means that the socket has
-- socket has been closed by peer. -- been closed by peer.
exit when Index < First or else Index = Max; exit when Index < First or else Index = Max;
...@@ -2230,8 +2226,8 @@ package body GNAT.Sockets is ...@@ -2230,8 +2226,8 @@ package body GNAT.Sockets is
loop loop
Send_Socket (Stream.Socket, Item (First .. Max), Index); Send_Socket (Stream.Socket, Item (First .. Max), Index);
-- Exit when all or zero data sent. Zero means that the -- Exit when all or zero data sent. Zero means that the socket has
-- socket has been closed by peer. -- been closed by peer.
exit when Index < First or else Index = Max; exit when Index < First or else Index = Max;
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
-- -- -- --
-- B o d y -- -- B o d y --
-- -- -- --
-- Copyright (C) 2001-2004 Ada Core Technologies, Inc. -- -- Copyright (C) 2001-2005 AdaCore --
-- -- -- --
-- GNAT is free software; you can redistribute it and/or modify it under -- -- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- -- -- terms of the GNU General Public License as published by the Free Soft- --
...@@ -409,6 +409,31 @@ package body GNAT.Sockets.Thin is ...@@ -409,6 +409,31 @@ package body GNAT.Sockets.Thin is
return Res; return Res;
end C_Select; end C_Select;
-----------------
-- C_Inet_Addr --
-----------------
function C_Inet_Addr
(Cp : C.Strings.chars_ptr) return C.int
is
use type C.unsigned_long;
function Internal_Inet_Addr
(Cp : C.Strings.chars_ptr) return C.unsigned_long;
pragma Import (Stdcall, Internal_Inet_Addr, "inet_addr");
Res : C.unsigned_long;
begin
Res := Internal_Inet_Addr (Cp);
if Res = C.unsigned_long'Last then
-- This value is returned in case of error
return -1;
else
return C.int (Internal_Inet_Addr (Cp));
end if;
end C_Inet_Addr;
-------------- --------------
-- C_Writev -- -- C_Writev --
-------------- --------------
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
-- -- -- --
-- S p e c -- -- S p e c --
-- -- -- --
-- Copyright (C) 2001-2004 Ada Core Technologies, Inc. -- -- Copyright (C) 2001-2005 AdaCore --
-- -- -- --
-- GNAT is free software; you can redistribute it and/or modify it under -- -- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- -- -- terms of the GNU General Public License as published by the Free Soft- --
...@@ -395,7 +395,6 @@ private ...@@ -395,7 +395,6 @@ private
pragma Import (Stdcall, C_Getservbyport, "getservbyport"); pragma Import (Stdcall, C_Getservbyport, "getservbyport");
pragma Import (Stdcall, C_Getsockname, "getsockname"); pragma Import (Stdcall, C_Getsockname, "getsockname");
pragma Import (Stdcall, C_Getsockopt, "getsockopt"); pragma Import (Stdcall, C_Getsockopt, "getsockopt");
pragma Import (Stdcall, C_Inet_Addr, "inet_addr");
pragma Import (Stdcall, C_Ioctl, "ioctlsocket"); pragma Import (Stdcall, C_Ioctl, "ioctlsocket");
pragma Import (Stdcall, C_Listen, "listen"); pragma Import (Stdcall, C_Listen, "listen");
pragma Import (Stdcall, C_Recv, "recv"); pragma Import (Stdcall, C_Recv, "recv");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment