001// JWR: This modification gets rid of nasty problems to do with unroutable hostnames.
002// OSX uses these a lot, and it is a total pain.
003// See my comments in stdlib.Trace.java
004//
005// In openjdk8, https://jdk8.java.net , this source is found in
006//
007//   openjdk8/jdk/src/share/classes/com/sun/tools/jdi/SocketTransportService.java
008
009/*
010 * Copyright (c) 1998, 2003, Oracle and/or its affiliates. All rights reserved.
011 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
012 *
013 * This code is free software; you can redistribute it and/or modify it
014 * under the terms of the GNU General Public License version 2 only, as
015 * published by the Free Software Foundation.  Oracle designates this
016 * particular file as subject to the "Classpath" exception as provided
017 * by Oracle in the LICENSE file that accompanied this code.
018 *
019 * This code is distributed in the hope that it will be useful, but WITHOUT
020 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
021 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
022 * version 2 for more details (a copy is included in the LICENSE file that
023 * accompanied this code).
024 *
025 * You should have received a copy of the GNU General Public License version
026 * 2 along with this work; if not, write to the Free Software Foundation,
027 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
028 *
029 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
030 * or visit www.oracle.com if you need additional information or have any
031 * questions.
032 */
033
034package com.sun.tools.jdi;
035
036import java.net.*;
037
038class SocketTransportService$SocketListenKey extends com.sun.jdi.connect.spi.TransportService.ListenKey {
039        ServerSocket ss;
040        SocketTransportService$SocketListenKey (ServerSocket ss) {
041                this.ss = ss;
042        }
043        ServerSocket socket () {
044                return ss;
045        }
046        public String toString () {
047                return address ();
048        }
049
050        // Returns the string representation of the address that this listen key represents.
051        public String address () {
052                InetAddress address = ss.getInetAddress ();
053
054                // If bound to the wildcard address then use current local hostname. In
055                // the event that we don't know our own hostname then assume that host
056                // supports IPv4 and return something to represent the loopback address.
057                if (address.isAnyLocalAddress ()) {
058                        // JWR: Only change is to comment out the lines below
059                        // try {
060                        //     address = InetAddress.getLocalHost ();
061                        // } catch (UnknownHostException uhe) {
062                        byte[] loopback = { 0x7f, 0x00, 0x00, 0x01 };
063                        try {
064                                address = InetAddress.getByAddress ("127.0.0.1", loopback);
065                        } catch (UnknownHostException x) {
066                                throw new InternalError ("unable to get local hostname");
067                        }
068                        //  }
069                }
070
071                // Now decide if we return a hostname or IP address. Where possible
072                // return a hostname but in the case that we are bound to an address
073                // that isn't registered in the name service then we return an address.
074                String result;
075                String hostname = address.getHostName ();
076                String hostaddr = address.getHostAddress ();
077                if (hostname.equals(hostaddr)) {
078                        if (address instanceof Inet6Address) {
079                                result = "[" + hostaddr + "]";
080                        } else {
081                                result = hostaddr;
082                        }
083                } else {
084                        result = hostname;
085                }
086
087                // Finally return "hostname:port", "ipv4-address:port" or "[ipv6-address]:port".
088                return result + ":" + ss.getLocalPort ();
089        }
090}