Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2615fd48 authored by Paul Jensen's avatar Paul Jensen
Browse files

Adjust Network's SocketFactory to restrict host name resolution to that Network

Change-Id: Iab9a5a2e060fe261f4be9ba974c1a55fb6b9c98b
parent 2416768a
Loading
Loading
Loading
Loading
+17 −2
Original line number Original line Diff line number Diff line
@@ -94,11 +94,26 @@ public class Network implements Parcelable {
            mNetId = netId;
            mNetId = netId;
        }
        }


        private void connectToHost(Socket socket, String host, int port) throws IOException {
            // Lookup addresses only on this Network.
            InetAddress[] hostAddresses = getAllByName(host);
            // Try all but last address ignoring exceptions.
            for (int i = 0; i < hostAddresses.length - 1; i++) {
                try {
                    socket.connect(new InetSocketAddress(hostAddresses[i], port));
                    return;
                } catch (IOException e) {
                }
            }
            // Try last address.  Do throw exceptions.
            socket.connect(new InetSocketAddress(hostAddresses[hostAddresses.length - 1], port));
        }

        @Override
        @Override
        public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException {
        public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException {
            Socket socket = createSocket();
            Socket socket = createSocket();
            socket.bind(new InetSocketAddress(localHost, localPort));
            socket.bind(new InetSocketAddress(localHost, localPort));
            socket.connect(new InetSocketAddress(host, port));
            connectToHost(socket, host, port);
            return socket;
            return socket;
        }
        }


@@ -121,7 +136,7 @@ public class Network implements Parcelable {
        @Override
        @Override
        public Socket createSocket(String host, int port) throws IOException {
        public Socket createSocket(String host, int port) throws IOException {
            Socket socket = createSocket();
            Socket socket = createSocket();
            socket.connect(new InetSocketAddress(host, port));
            connectToHost(socket, host, port);
            return socket;
            return socket;
        }
        }