671N/A/*
2362N/A * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
671N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
671N/A *
671N/A * This code is free software; you can redistribute it and/or modify it
671N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
671N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
671N/A *
671N/A * This code is distributed in the hope that it will be useful, but WITHOUT
671N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
671N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
671N/A * version 2 for more details (a copy is included in the LICENSE file that
671N/A * accompanied this code).
671N/A *
671N/A * You should have received a copy of the GNU General Public License version
671N/A * 2 along with this work; if not, write to the Free Software Foundation,
671N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
671N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
671N/A */
671N/A
671N/Apackage sun.management.jmxremote;
671N/A
671N/Aimport java.io.IOException;
671N/Aimport java.net.InetAddress;
671N/Aimport java.net.NetworkInterface;
671N/Aimport java.net.ServerSocket;
671N/Aimport java.net.Socket;
671N/Aimport java.net.SocketException;
671N/Aimport java.rmi.server.RMIServerSocketFactory;
671N/Aimport java.util.Enumeration;
671N/A
671N/A/**
671N/A * This RMI server socket factory creates server sockets that
671N/A * will only accept connection requests from clients running
671N/A * on the host where the RMI remote objects have been exported.
671N/A */
671N/Apublic final class LocalRMIServerSocketFactory implements RMIServerSocketFactory {
671N/A /**
671N/A * Creates a server socket that only accepts connection requests from
671N/A * clients running on the host where the RMI remote objects have been
671N/A * exported.
671N/A */
671N/A public ServerSocket createServerSocket(int port) throws IOException {
671N/A return new ServerSocket(port) {
671N/A @Override
671N/A public Socket accept() throws IOException {
716N/A final Socket socket = super.accept();
716N/A final InetAddress remoteAddr = socket.getInetAddress();
671N/A final String msg = "The server sockets created using the " +
716N/A "LocalRMIServerSocketFactory only accept connections " +
716N/A "from clients running on the host where the RMI " +
716N/A "remote objects have been exported.";
716N/A
716N/A if (remoteAddr == null) {
716N/A // Though unlikeky, the socket could be already
716N/A // closed... Send a more detailed message in
716N/A // this case. Also avoid throwing NullPointerExceptiion
716N/A //
716N/A String details = "";
716N/A if (socket.isClosed()) {
716N/A details = " Socket is closed.";
716N/A } else if (!socket.isConnected()) {
716N/A details = " Socket is not connected";
716N/A }
716N/A try {
716N/A socket.close();
716N/A } catch (Exception ok) {
716N/A // ok - this is just cleanup before throwing detailed
716N/A // exception.
716N/A }
716N/A throw new IOException(msg +
716N/A " Couldn't determine client address." +
716N/A details);
716N/A } else if (remoteAddr.isLoopbackAddress()) {
671N/A // local address: accept the connection.
671N/A return socket;
671N/A }
671N/A // Retrieve all the network interfaces on this host.
671N/A Enumeration<NetworkInterface> nis;
671N/A try {
671N/A nis = NetworkInterface.getNetworkInterfaces();
671N/A } catch (SocketException e) {
671N/A try {
671N/A socket.close();
671N/A } catch (IOException ioe) {
671N/A // Ignore...
671N/A }
671N/A throw new IOException(msg, e);
671N/A }
671N/A // Walk through the network interfaces to see
671N/A // if any of them matches the client's address.
671N/A // If true, then the client's address is local.
671N/A while (nis.hasMoreElements()) {
671N/A NetworkInterface ni = nis.nextElement();
671N/A Enumeration<InetAddress> addrs = ni.getInetAddresses();
671N/A while (addrs.hasMoreElements()) {
671N/A InetAddress localAddr = addrs.nextElement();
671N/A if (localAddr.equals(remoteAddr)) {
671N/A return socket;
671N/A }
671N/A }
671N/A }
671N/A // The client's address is remote so refuse the connection.
671N/A try {
671N/A socket.close();
671N/A } catch (IOException ioe) {
671N/A // Ignore...
671N/A }
671N/A throw new IOException(msg);
671N/A }
671N/A };
671N/A }
671N/A
671N/A /**
671N/A * Two LocalRMIServerSocketFactory objects
671N/A * are equal if they are of the same type.
671N/A */
671N/A @Override
671N/A public boolean equals(Object obj) {
671N/A return (obj instanceof LocalRMIServerSocketFactory);
671N/A }
671N/A
671N/A /**
671N/A * Returns a hash code value for this LocalRMIServerSocketFactory.
671N/A */
671N/A @Override
671N/A public int hashCode() {
671N/A return getClass().hashCode();
671N/A }
671N/A}