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

Unverified Commit 28b1fe97 authored by Tobias Kaminsky's avatar Tobias Kaminsky Committed by GitHub
Browse files

Merge pull request #105 from nextcloud/ipv6

Add support for IPv6
parents fca8d44c 7f6689dd
Loading
Loading
Loading
Loading
+119 −101
Original line number Diff line number Diff line
@@ -24,7 +24,17 @@

package com.owncloud.android.lib.common.network;

import com.owncloud.android.lib.common.utils.Log_OC;

import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
import org.apache.http.conn.ssl.X509HostnameVerifier;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
@@ -41,15 +51,6 @@ import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;

import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
import org.apache.http.conn.ssl.X509HostnameVerifier;

import com.owncloud.android.lib.common.utils.Log_OC;



/**
 * AdvancedSSLProtocolSocketFactory allows to create SSL {@link Socket}s with
@@ -66,10 +67,6 @@ public class AdvancedSslSocketFactory implements SecureProtocolSocketFactory {
    private AdvancedX509TrustManager mTrustManager = null;
    private X509HostnameVerifier mHostnameVerifier = null;

    public SSLContext getSslContext() {
        return mSslContext;
    }
    
    /**
     * Constructor for AdvancedSSLProtocolSocketFactory.
     */
@@ -89,14 +86,19 @@ public class AdvancedSslSocketFactory implements SecureProtocolSocketFactory {
        mHostnameVerifier = hostnameVerifier;
    }

    public SSLContext getSslContext() {
        return mSslContext;
    }

    /**
     * @see ProtocolSocketFactory#createSocket(java.lang.String, int, java.net.InetAddress, int)
     */
    @Override
    public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort)
    		throws IOException, UnknownHostException {
            throws IOException {

        Socket socket = mSslContext.getSocketFactory().createSocket(host, port, clientHost, clientPort);
        Socket socket = mSslContext.getSocketFactory().createSocket(getInetAddressForHost(host), port, clientHost,
                clientPort);
        enableSecureProtocols(socket);
        verifyPeerIdentity(host, port, socket);
        return socket;
@@ -151,9 +153,7 @@ public class AdvancedSslSocketFactory implements SecureProtocolSocketFactory {
     * @param clientHost the local host name/IP to bind the socket to
     * @param clientPort the port on the local machine
     * @param params     {@link HttpConnectionParams Http connection parameters}
     * 
     * @return Socket a new socket
     * 
     * @throws IOException          if an I/O error occurs while creating the socket
     * @throws UnknownHostException if the IP address of the host cannot be
     *                              determined
@@ -177,7 +177,9 @@ public class AdvancedSslSocketFactory implements SecureProtocolSocketFactory {
        Socket socket = socketfactory.createSocket();
        enableSecureProtocols(socket);
        SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);


        SocketAddress remoteaddr = new InetSocketAddress(getInetAddressForHost(host), port);
        socket.setSoTimeout(params.getSoTimeout() * 5);
        socket.bind(localaddr);
        ServerNameIndicator.setServerNameIndication(host, (SSLSocket) socket);
@@ -186,6 +188,21 @@ public class AdvancedSslSocketFactory implements SecureProtocolSocketFactory {
        return socket;
    }

    private InetAddress getInetAddressForHost(String host) throws UnknownHostException {
        InetAddress address = InetAddress.getByName(host);
        if (address instanceof Inet6Address) {
            InetAddress[] inetAddressArray = InetAddress.getAllByName(host);
            for (InetAddress inetAddress : inetAddressArray) {
                if (inetAddress instanceof Inet4Address) {
                    address = inetAddress;
                    break;
                }
            }
        }

        return address;
    }

    /**
     * @see ProtocolSocketFactory#createSocket(java.lang.String, int)
     */
@@ -193,7 +210,7 @@ public class AdvancedSslSocketFactory implements SecureProtocolSocketFactory {
    public Socket createSocket(String host, int port) throws IOException,
            UnknownHostException {
        Log_OC.d(TAG, "Creating SSL Socket with remote " + host + ":" + port);
        Socket socket = mSslContext.getSocketFactory().createSocket(host, port);
        Socket socket = mSslContext.getSocketFactory().createSocket(getInetAddressForHost(host), port);
        enableSecureProtocols(socket);
        verifyPeerIdentity(host, port, socket);
        return socket;
@@ -236,6 +253,7 @@ public class AdvancedSslSocketFactory implements SecureProtocolSocketFactory {
     *
     * Then, the host name is compared with the content of the server certificate using the current host name verifier,
     * if any.
     *
     * @param socket
     */
    private void verifyPeerIdentity(String host, int port, Socket socket) throws IOException {