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

Commit f0382899 authored by Lorenzo Colitti's avatar Lorenzo Colitti Committed by Paul Jensen
Browse files

Use a new socket for each of the host's IP addresses.

If Socket.connect() times out, the socket cannot be used any
more - any attempt to do so fails with EBADF. Use a new
socket for each IP address.

Bug: 16664129
Change-Id: If3616df86f7c2da0eabd30dca5db65d0da85cb17
parent af4d04ca
Loading
Loading
Loading
Loading
+12 −13
Original line number Original line Diff line number Diff line
@@ -25,6 +25,7 @@ import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.net.UnknownHostException;
import java.net.URL;
import java.net.URL;
@@ -106,27 +107,27 @@ public class Network implements Parcelable {
            mNetId = netId;
            mNetId = netId;
        }
        }


        private void connectToHost(Socket socket, String host, int port) throws IOException {
        private Socket connectToHost(String host, int port, SocketAddress localAddress)
                throws IOException {
            // Lookup addresses only on this Network.
            // Lookup addresses only on this Network.
            InetAddress[] hostAddresses = getAllByName(host);
            InetAddress[] hostAddresses = getAllByName(host);
            // Try all but last address ignoring exceptions.
            // Try all addresses.
            for (int i = 0; i < hostAddresses.length - 1; i++) {
            for (int i = 0; i < hostAddresses.length; i++) {
                try {
                try {
                    Socket socket = createSocket();
                    if (localAddress != null) socket.bind(localAddress);
                    socket.connect(new InetSocketAddress(hostAddresses[i], port));
                    socket.connect(new InetSocketAddress(hostAddresses[i], port));
                    return;
                    return socket;
                } catch (IOException e) {
                } catch (IOException e) {
                    if (i == (hostAddresses.length - 1)) throw e;
                }
                }
            }
            }
            // Try last address.  Do throw exceptions.
            throw new UnknownHostException(host);
            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();
            return connectToHost(host, port, new InetSocketAddress(localHost, localPort));
            socket.bind(new InetSocketAddress(localHost, localPort));
            connectToHost(socket, host, port);
            return socket;
        }
        }


        @Override
        @Override
@@ -147,9 +148,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();
            return connectToHost(host, port, null);
            connectToHost(socket, host, port);
            return socket;
        }
        }


        @Override
        @Override
+3 −2
Original line number Original line Diff line number Diff line
@@ -630,12 +630,13 @@ public class NetworkMonitor extends StateMachine {
                urlConnection.getInputStream();
                urlConnection.getInputStream();
                httpResponseCode = urlConnection.getResponseCode();
                httpResponseCode = urlConnection.getResponseCode();
            } else {
            } else {
                socket = mNetworkAgentInfo.network.getSocketFactory().createSocket();
                socket.setSoTimeout(SOCKET_TIMEOUT_MS);
                // Lookup addresses only on this Network.
                // Lookup addresses only on this Network.
                InetAddress[] hostAddresses = mNetworkAgentInfo.network.getAllByName(url.getHost());
                InetAddress[] hostAddresses = mNetworkAgentInfo.network.getAllByName(url.getHost());
                // Try all addresses.
                // Try all addresses.
                for (int i = 0; i < hostAddresses.length; i++) {
                for (int i = 0; i < hostAddresses.length; i++) {
                    // Create a new socket for every IP address. See http://b/16664129 .
                    socket = mNetworkAgentInfo.network.getSocketFactory().createSocket();
                    socket.setSoTimeout(SOCKET_TIMEOUT_MS);
                    if (DBG) log("Connecting to " + hostAddresses[i]);
                    if (DBG) log("Connecting to " + hostAddresses[i]);
                    try {
                    try {
                        socket.connect(new InetSocketAddress(hostAddresses[i],
                        socket.connect(new InetSocketAddress(hostAddresses[i],