gcc -v reports:
Reading specs from /usr/lib/gcc/i686-pc-linux-gnu/3.4.6/specs
Configured with: /var/tmp/portage/gcc-3.4.6-r1/work/gcc-3.4.6/configure
--prefix=/usr --bindir=/usr/i686-pc-linux-gnu/gcc-bin/3.4.6
--includedir=/usr/lib/gcc/i686-pc-linux-gnu/3.4.6/include
--datadir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4.6
--mandir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4.6/man
--infodir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4.6/info
--with-gxx-include-dir=/usr/lib/gcc/i686-pc-linux-gnu/3.4.6/include/g++-v3
--host=i686-pc-linux-gnu --build=i686-pc-linux-gnu --disable-altivec
--enable-nls --without-included-gettext --with-system-zlib --disable-checking
--disable-werror --disable-libunwind-exceptions --disable-multilib
--disable-libgcj --enable-languages=c,c++,objc,f77 --enable-shared
--enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu
Thread model: posix
gcc version 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)

GNAT has been built from gnat-gpl portage.

Bug description:
Bind_Socket should bind socket to address specified in Sock_Addr_Type
structure. Sock_Addr_Type stores Address and Port. Bind_Socket doesn't use
Address field of Sock_Addr_Type record. This results in binding to all
interfaces (address: 0.0.0.0).

Here is an example program:

with Ada.Text_IO; use Ada.Text_IO;
with GNAT.Sockets; use GNAT.Sockets;

procedure Bind_Socket_Bug is
   Server_Socket  : Socket_Type;
   Client_Socket  : Socket_Type;
   Accepted_Socket: Socket_Type;
   Socket_Address : Sock_Addr_Type;

   function Image (Socket_Address   : Sock_Addr_Type) return String is
   begin
      return Image (Socket_Address.Addr) & ":" & Socket_Address.Port'Img;
   end Image;

begin
   Create_Socket (Server_Socket);
   Socket_Address.Addr := Inet_Addr ("127.0.0.1");
   Socket_Address.Port := Any_Port;
   --  Should bind to 127.0.0.1 only, binds to all interfaces
   Bind_Socket (Server_Socket, Socket_Address);
   Socket_Address := Get_Socket_Name (Server_Socket);
   Put_Line ("Socket bound to: " & Image (Socket_Address));
   Listen_Socket (Server_Socket);


   --  Insert your address here, other than 127.0.0.1
   Socket_Address.Addr := Inet_Addr ("127.0.0.2");

   Put_Line ("Connecting to: " & Image (Socket_Address));
   Create_Socket (Client_Socket);
   Connect_Socket (Client_Socket, Socket_Address);

   Accept_Socket (Server_Socket, Accepted_Socket, Socket_Address);
   Put_Line ("Accepted connection from: " & Image (Socket_Address));

   Close_Socket (Accepted_Socket);
   Close_Socket (Client_Socket);
   Close_Socket (Server_Socket);
end Bind_Socket_Bug;

Compiled like this:
$ gnatmake bind_socket_bug.adb 
gnatgcc -c bind_socket_bug.adb
gnatbind -x bind_socket_bug.ali
gnatlink bind_socket_bug.ali

Expected behaviour:
Socket bound to: 127.0.0.1: 48143
Connecting to: 127.0.0.2: 48143
<-- here the connection should be refused -->

Actual behaviour:
Scoket bound to: 0.0.0.0: 48143
Connecting to: 127.0.0.2: 48143
Accepted connection from: 127.0.0.2: 40744

The problem is also present in CVS version (I checked the repository on the
web).


-- 
           Summary: Bind_Socket doesn't bind to specified address
           Product: gcc
           Version: 3.4.6
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: ada
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: msimonides at power dot com dot pl
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28716

Reply via email to