1 /* 2 * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * @bug 8195160 26 * @summary REGRESSION: Socket.getLocalAddress() returns address of 0.0.0.0 on outbound TCP 27 * @requires (os.family == "linux") 28 * @library .. /test/lib 29 * @build RsocketTest 30 * @run main/othervm -Djava.net.preferIPv4Stack=true GetLocalAddress 31 */ 32 import java.util.*; 33 import java.net.*; 34 import jdk.net.Sockets; 35 36 public class GetLocalAddress implements Runnable { 37 38 static Socket s; 39 static ServerSocket ss; 40 static InetAddress addr; 41 static InetAddress iaLocal; 42 static String sLocalHostname; 43 44 public static void main(String[] args) throws Exception 45 { 46 if (!RsocketTest.isRsocketAvailable()) 47 return; 48 49 ss = Sockets.openRdmaServerSocket(); 50 addr = InetAddress.getLocalHost(); 51 ss.bind(new InetSocketAddress(addr, 0)); 52 int port = ss.getLocalPort(); 53 54 s = Sockets.openRdmaSocket(); 55 56 try { 57 Thread t = new Thread(new GetLocalAddress()); 58 t.start(); 59 } catch(Exception e) { 60 System.out.println("Exception happened"); 61 throw e; 62 } finally { 63 ss.close(); 64 } 65 } 66 67 public void run() { 68 try { 69 InetSocketAddress isa = new InetSocketAddress(addr, 0); 70 s.connect( isa, 1000 ); 71 72 byte[] bad = {0,0,0,0}; 73 InetAddress iaLocal = s.getLocalAddress(); 74 String sLocalHostname = iaLocal.getHostName(); 75 if (Arrays.equals (iaLocal.getAddress(), bad)) { 76 throw new RuntimeException ("0.0.0.0 returned"); 77 } 78 System.out.println("local hostname is "+sLocalHostname ); 79 } catch (Exception e) { 80 e.printStackTrace(); 81 throw new RuntimeException("Test Failed!"); 82 } 83 } 84 }