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

Commit 32a58f00 authored by Paul Jensen's avatar Paul Jensen
Browse files

Use return values from JNI functions binding sockets and processes to networks.

bug:15757549
Change-Id: If23b14febc923b9a0348f0cf9029fd4bf6e8d725
parent 9a229720
Loading
Loading
Loading
Loading
+4 −8
Original line number Diff line number Diff line
@@ -2372,12 +2372,10 @@ public class ConnectivityManager {
     */
    public static boolean setProcessDefaultNetwork(Network network) {
        if (network == null) {
            NetworkUtils.unbindProcessToNetwork();
            return NetworkUtils.unbindProcessToNetwork();
        } else {
            NetworkUtils.bindProcessToNetwork(network.netId);
            return NetworkUtils.bindProcessToNetwork(network.netId);
        }
        // TODO fix return value
        return true;
    }

    /**
@@ -2404,11 +2402,9 @@ public class ConnectivityManager {
     */
    public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
        if (network == null) {
            NetworkUtils.unbindProcessToNetworkForHostResolution();
            return NetworkUtils.unbindProcessToNetworkForHostResolution();
        } else {
            NetworkUtils.bindProcessToNetworkForHostResolution(network.netId);
            return NetworkUtils.bindProcessToNetworkForHostResolution(network.netId);
        }
        // TODO hook up the return value.
        return true;
    }
}
+4 −1
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import javax.net.SocketFactory;

@@ -148,7 +149,9 @@ public class Network implements Parcelable {
            // Query a property of the underlying socket to ensure the underlying
            // socket exists so a file descriptor is available to bind to a network.
            socket.getReuseAddress();
            NetworkUtils.bindSocketToNetwork(socket.getFileDescriptor$().getInt$(), mNetId);
            if (!NetworkUtils.bindSocketToNetwork(socket.getFileDescriptor$().getInt$(), mNetId)) {
                throw new SocketException("Failed to bind socket to network.");
            }
            return socket;
        }
    }
+5 −5
Original line number Diff line number Diff line
@@ -118,13 +118,13 @@ public class NetworkUtils {
     * is by design so an application doesn't accidentally use sockets it thinks are still bound to
     * a particular {@code Network}.
     */
    public native static void bindProcessToNetwork(int netId);
    public native static boolean bindProcessToNetwork(int netId);

    /**
     * Clear any process specific {@code Network} binding.  This reverts a call to
     * {@link #bindProcessToNetwork}.
     */
    public native static void unbindProcessToNetwork();
    public native static boolean unbindProcessToNetwork();

    /**
     * Return the netId last passed to {@link #bindProcessToNetwork}, or NETID_UNSET if
@@ -138,7 +138,7 @@ public class NetworkUtils {
     *
     * @deprecated This is strictly for legacy usage to support startUsingNetworkFeature().
     */
    public native static void bindProcessToNetworkForHostResolution(int netId);
    public native static boolean bindProcessToNetworkForHostResolution(int netId);

    /**
     * Clears any process specific {@link Network} binding for host resolution.  This does
@@ -146,13 +146,13 @@ public class NetworkUtils {
     *
     * @deprecated This is strictly for legacy usage to support startUsingNetworkFeature().
     */
    public native static void unbindProcessToNetworkForHostResolution();
    public native static boolean unbindProcessToNetworkForHostResolution();

    /**
     * Explicitly binds {@code socketfd} to the network designated by {@code netId}.  This
     * overrides any binding via {@link #bindProcessToNetwork}.
     */
    public native static void bindSocketToNetwork(int socketfd, int netId);
    public native static boolean bindSocketToNetwork(int socketfd, int netId);

    /**
     * Convert a IPv4 address from an integer to an InetAddress.
+19 −16
Original line number Diff line number Diff line
@@ -214,7 +214,8 @@ static jboolean android_net_utils_runDhcp(JNIEnv* env, jobject clazz, jstring if
    return android_net_utils_runDhcpCommon(env, clazz, ifname, info, false);
}

static jboolean android_net_utils_runDhcpRenew(JNIEnv* env, jobject clazz, jstring ifname, jobject info)
static jboolean android_net_utils_runDhcpRenew(JNIEnv* env, jobject clazz, jstring ifname,
        jobject info)
{
    return android_net_utils_runDhcpCommon(env, clazz, ifname, info, true);
}
@@ -252,14 +253,14 @@ static void android_net_utils_markSocket(JNIEnv *env, jobject thiz, jint socket,
    }
}

static void android_net_utils_bindProcessToNetwork(JNIEnv *env, jobject thiz, jint netId)
static jboolean android_net_utils_bindProcessToNetwork(JNIEnv *env, jobject thiz, jint netId)
{
    setNetworkForProcess(netId);
    return (jboolean) !setNetworkForProcess(netId);
}

static void android_net_utils_unbindProcessToNetwork(JNIEnv *env, jobject thiz)
static jboolean android_net_utils_unbindProcessToNetwork(JNIEnv *env, jobject thiz)
{
    setNetworkForProcess(NETID_UNSET);
    return (jboolean) !setNetworkForProcess(NETID_UNSET);
}

static jint android_net_utils_getNetworkBoundToProcess(JNIEnv *env, jobject thiz)
@@ -267,19 +268,21 @@ static jint android_net_utils_getNetworkBoundToProcess(JNIEnv *env, jobject thiz
    return getNetworkForProcess();
}

static void android_net_utils_bindProcessToNetworkForHostResolution(JNIEnv *env, jobject thiz, jint netId)
static jboolean android_net_utils_bindProcessToNetworkForHostResolution(JNIEnv *env, jobject thiz,
        jint netId)
{
    setNetworkForResolv(netId);
    return (jboolean) !setNetworkForResolv(netId);
}

static void android_net_utils_unbindProcessToNetworkForHostResolution(JNIEnv *env, jobject thiz)
static jboolean android_net_utils_unbindProcessToNetworkForHostResolution(JNIEnv *env, jobject thiz)
{
    setNetworkForResolv(NETID_UNSET);
    return (jboolean) !setNetworkForResolv(NETID_UNSET);
}

static void android_net_utils_bindSocketToNetwork(JNIEnv *env, jobject thiz, jint socket, jint netId)
static jboolean android_net_utils_bindSocketToNetwork(JNIEnv *env, jobject thiz, jint socket,
        jint netId)
{
    setNetworkForSocket(netId, socket);
    return (jboolean) !setNetworkForSocket(netId, socket);
}

// ----------------------------------------------------------------------------
@@ -299,12 +302,12 @@ static JNINativeMethod gNetworkUtilMethods[] = {
    { "releaseDhcpLease", "(Ljava/lang/String;)Z",  (void *)android_net_utils_releaseDhcpLease },
    { "getDhcpError", "()Ljava/lang/String;", (void*) android_net_utils_getDhcpError },
    { "markSocket", "(II)V", (void*) android_net_utils_markSocket },
    { "bindProcessToNetwork", "(I)V", (void*) android_net_utils_bindProcessToNetwork },
    { "bindProcessToNetwork", "(I)Z", (void*) android_net_utils_bindProcessToNetwork },
    { "getNetworkBoundToProcess", "()I", (void*) android_net_utils_getNetworkBoundToProcess },
    { "unbindProcessToNetwork", "()V", (void*) android_net_utils_unbindProcessToNetwork },
    { "bindProcessToNetworkForHostResolution", "(I)V", (void*) android_net_utils_bindProcessToNetworkForHostResolution },
    { "unbindProcessToNetworkForHostResolution", "()V", (void*) android_net_utils_unbindProcessToNetworkForHostResolution },
    { "bindSocketToNetwork", "(II)V", (void*) android_net_utils_bindSocketToNetwork },
    { "unbindProcessToNetwork", "()Z", (void*) android_net_utils_unbindProcessToNetwork },
    { "bindProcessToNetworkForHostResolution", "(I)Z", (void*) android_net_utils_bindProcessToNetworkForHostResolution },
    { "unbindProcessToNetworkForHostResolution", "()Z", (void*) android_net_utils_unbindProcessToNetworkForHostResolution },
    { "bindSocketToNetwork", "(II)Z", (void*) android_net_utils_bindSocketToNetwork },
};

int register_android_net_NetworkUtils(JNIEnv* env)