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

Commit aa70f101 authored by Robert Greenwalt's avatar Robert Greenwalt
Browse files

Add RouteInfo objects for tracking routes.

Used to have list of gateways for default routes, but general static routes
should be supported.

Change-Id: I01730142c6139f2b833b9d48f5381d2d320b69f6
parent 96974931
Loading
Loading
Loading
Loading
+20 −5
Original line number Diff line number Diff line
@@ -22,6 +22,8 @@ import android.util.Log;
import java.net.InetAddress;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;

/**
 * A simple object for retrieving the results of a DHCP request.
@@ -31,7 +33,6 @@ import java.net.UnknownHostException;
public class DhcpInfoInternal {
    private final static String TAG = "DhcpInfoInternal";
    public String ipAddress;
    public String gateway;
    public int prefixLength;

    public String dns1;
@@ -40,7 +41,14 @@ public class DhcpInfoInternal {
    public String serverAddress;
    public int leaseDuration;

    private Collection<RouteInfo> routes;

    public DhcpInfoInternal() {
        routes = new ArrayList<RouteInfo>();
    }

    public void addRoute(RouteInfo routeInfo) {
        routes.add(routeInfo);
    }

    private int convertToInt(String addr) {
@@ -58,7 +66,12 @@ public class DhcpInfoInternal {
    public DhcpInfo makeDhcpInfo() {
        DhcpInfo info = new DhcpInfo();
        info.ipAddress = convertToInt(ipAddress);
        info.gateway = convertToInt(gateway);
        for (RouteInfo route : routes) {
            if (route.isDefaultRoute()) {
                info.gateway = convertToInt(route.getGateway().getHostAddress());
                break;
            }
        }
        try {
            InetAddress inetAddress = NetworkUtils.numericToInetAddress(ipAddress);
            info.netmask = NetworkUtils.prefixLengthToNetmaskInt(prefixLength);
@@ -81,8 +94,8 @@ public class DhcpInfoInternal {
    public LinkProperties makeLinkProperties() {
        LinkProperties p = new LinkProperties();
        p.addLinkAddress(makeLinkAddress());
        if (TextUtils.isEmpty(gateway) == false) {
            p.addGateway(NetworkUtils.numericToInetAddress(gateway));
        for (RouteInfo route : routes) {
            p.addRoute(route);
        }
        if (TextUtils.isEmpty(dns1) == false) {
            p.addDns(NetworkUtils.numericToInetAddress(dns1));
@@ -98,8 +111,10 @@ public class DhcpInfoInternal {
    }

    public String toString() {
        String routeString = "";
        for (RouteInfo route : routes) routeString += route.toString() + " | ";
        return "addr: " + ipAddress + "/" + prefixLength +
                " gateway: " + gateway +
                " routes: " + routeString +
                " dns: " + dns1 + "," + dns2 +
                " dhcpServer: " + serverAddress +
                " leaseDuration: " + leaseDuration;
+21 −23
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ public class LinkProperties implements Parcelable {
    String mIfaceName;
    private Collection<LinkAddress> mLinkAddresses;
    private Collection<InetAddress> mDnses;
    private Collection<InetAddress> mGateways;
    private Collection<RouteInfo> mRoutes;
    private ProxyProperties mHttpProxy;

    public LinkProperties() {
@@ -67,7 +67,7 @@ public class LinkProperties implements Parcelable {
            mIfaceName = source.getInterfaceName();
            mLinkAddresses = source.getLinkAddresses();
            mDnses = source.getDnses();
            mGateways = source.getGateways();
            mRoutes = source.getRoutes();
            mHttpProxy = new ProxyProperties(source.getHttpProxy());
        }
    }
@@ -104,11 +104,11 @@ public class LinkProperties implements Parcelable {
        return Collections.unmodifiableCollection(mDnses);
    }

    public void addGateway(InetAddress gateway) {
        if (gateway != null) mGateways.add(gateway);
    public void addRoute(RouteInfo route) {
        if (route != null) mRoutes.add(route);
    }
    public Collection<InetAddress> getGateways() {
        return Collections.unmodifiableCollection(mGateways);
    public Collection<RouteInfo> getRoutes() {
        return Collections.unmodifiableCollection(mRoutes);
    }

    public void setHttpProxy(ProxyProperties proxy) {
@@ -122,7 +122,7 @@ public class LinkProperties implements Parcelable {
        mIfaceName = null;
        mLinkAddresses = new ArrayList<LinkAddress>();
        mDnses = new ArrayList<InetAddress>();
        mGateways = new ArrayList<InetAddress>();
        mRoutes = new ArrayList<RouteInfo>();
        mHttpProxy = null;
    }

@@ -146,12 +146,12 @@ public class LinkProperties implements Parcelable {
        for (InetAddress addr : mDnses) dns += addr.getHostAddress() + ",";
        dns += "] ";

        String gateways = "Gateways: [";
        for (InetAddress gw : mGateways) gateways += gw.getHostAddress() + ",";
        gateways += "] ";
        String routes = "Routes: [";
        for (RouteInfo route : mRoutes) routes += route.toString() + ",";
        routes += "] ";
        String proxy = (mHttpProxy == null ? "" : "HttpProxy: " + mHttpProxy.toString() + " ");

        return ifaceName + linkAddresses + gateways + dns + proxy;
        return ifaceName + linkAddresses + routes + dns + proxy;
    }


@@ -177,7 +177,7 @@ public class LinkProperties implements Parcelable {

        boolean sameAddresses;
        boolean sameDnses;
        boolean sameGateways;
        boolean sameRoutes;

        LinkProperties target = (LinkProperties) obj;

@@ -190,12 +190,12 @@ public class LinkProperties implements Parcelable {
        sameDnses = (mDnses.size() == targetDnses.size()) ?
                mDnses.containsAll(targetDnses) : false;

        Collection<InetAddress> targetGateways = target.getGateways();
        sameGateways = (mGateways.size() == targetGateways.size()) ?
                mGateways.containsAll(targetGateways) : false;
        Collection<RouteInfo> targetRoutes = target.getRoutes();
        sameRoutes = (mRoutes.size() == targetRoutes.size()) ?
                mRoutes.containsAll(targetRoutes) : false;

        return
            sameAddresses && sameDnses && sameGateways
            sameAddresses && sameDnses && sameRoutes
            && TextUtils.equals(getInterfaceName(), target.getInterfaceName())
            && (getHttpProxy() == null ? target.getHttpProxy() == null :
                getHttpProxy().equals(target.getHttpProxy()));
@@ -211,7 +211,7 @@ public class LinkProperties implements Parcelable {
        return ((null == mIfaceName) ? 0 : mIfaceName.hashCode()
                + mLinkAddresses.size() * 31
                + mDnses.size() * 37
                + mGateways.size() * 41
                + mRoutes.size() * 41
                + ((null == mHttpProxy) ? 0 : mHttpProxy.hashCode()));
    }

@@ -231,9 +231,9 @@ public class LinkProperties implements Parcelable {
            dest.writeByteArray(d.getAddress());
        }

        dest.writeInt(mGateways.size());
        for(InetAddress gw : mGateways) {
            dest.writeByteArray(gw.getAddress());
        dest.writeInt(mRoutes.size());
        for(RouteInfo route : mRoutes) {
            dest.writeParcelable(route, flags);
        }

        if (mHttpProxy != null) {
@@ -272,9 +272,7 @@ public class LinkProperties implements Parcelable {
                }
                addressCount = in.readInt();
                for (int i=0; i<addressCount; i++) {
                    try {
                        netProp.addGateway(InetAddress.getByAddress(in.createByteArray()));
                    } catch (UnknownHostException e) { }
                    netProp.addRoute((RouteInfo)in.readParcelable(null));
                }
                if (in.readByte() == 1) {
                    netProp.setHttpProxy((ProxyProperties)in.readParcelable(null));
+19 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.net;

parcelable RouteInfo;
+162 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.net;

import android.os.Parcel;
import android.os.Parcelable;

import java.net.UnknownHostException;
import java.net.InetAddress;
import java.net.Inet4Address;
import java.net.Inet6Address;
/**
 * A simple container for route information.
 *
 * @hide
 */
public class RouteInfo implements Parcelable {
    /**
     * The IP destination address for this route.
     */
    private final LinkAddress mDestination;

    /**
     * The gateway address for this route.
     */
    private final InetAddress mGateway;

    private final boolean mIsDefault;

    public RouteInfo(LinkAddress destination, InetAddress gateway) {
        if (destination == null) {
            try {
                if ((gateway != null) && (gateway instanceof Inet4Address)) {
                    destination = new LinkAddress(InetAddress.getByName("0.0.0.0"), 32);
                } else {
                    destination = new LinkAddress(InetAddress.getByName("::0"), 128);
                }
            } catch (Exception e) {}
        }
        mDestination = destination;
        mGateway = gateway;
        mIsDefault = isDefault();
    }

    public RouteInfo(InetAddress gateway) {
        LinkAddress destination = null;
        try {
            if ((gateway != null) && (gateway instanceof Inet4Address)) {
                destination = new LinkAddress(InetAddress.getByName("0.0.0.0"), 32);
            } else {
                destination = new LinkAddress(InetAddress.getByName("::0"), 128);
            }
        } catch (Exception e) {}
        mDestination = destination;
        mGateway = gateway;
        mIsDefault = isDefault();
    }

    private boolean isDefault() {
        boolean val = false;
        if (mGateway != null) {
            if (mGateway instanceof Inet4Address) {
                val = (mDestination == null || mDestination.getNetworkPrefixLength() == 32);
            } else {
                val = (mDestination == null || mDestination.getNetworkPrefixLength() == 128);
            }
        }
        return val;
    }

    public LinkAddress getDestination() {
        return mDestination;
    }

    public InetAddress getGateway() {
        return mGateway;
    }

    public boolean isDefaultRoute() {
        return mIsDefault;
    }

    public String toString() {
        String val = "";
        if (mDestination != null) val = mDestination.toString();
        if (mGateway != null) val += " -> " + mGateway.getHostAddress();
        return val;
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        if (mDestination == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeByteArray(mDestination.getAddress().getAddress());
            dest.writeInt(mDestination.getNetworkPrefixLength());
        }

        if (mGateway == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeByteArray(mGateway.getAddress());
        }
    }

    public static final Creator<RouteInfo> CREATOR =
        new Creator<RouteInfo>() {
        public RouteInfo createFromParcel(Parcel in) {
            InetAddress destAddr = null;
            int prefix = 0;
            InetAddress gateway = null;

            if (in.readByte() == 1) {
                byte[] addr = in.createByteArray();
                prefix = in.readInt();

                try {
                    destAddr = InetAddress.getByAddress(addr);
                } catch (UnknownHostException e) {}
            }

            if (in.readByte() == 1) {
                byte[] addr = in.createByteArray();

                try {
                    gateway = InetAddress.getByAddress(addr);
                } catch (UnknownHostException e) {}
            }

            LinkAddress dest = null;

            if (destAddr != null) {
                dest = new LinkAddress(destAddr, prefix);
            }

            return new RouteInfo(dest, gateway);
        }

        public RouteInfo[] newArray(int size) {
            return new RouteInfo[size];
        }
    };
}
+24 −3
Original line number Diff line number Diff line
@@ -58,7 +58,6 @@ static struct fieldIds {
    jclass dhcpInfoInternalClass;
    jmethodID constructorId;
    jfieldID ipaddress;
    jfieldID gateway;
    jfieldID prefixLength;
    jfieldID dns1;
    jfieldID dns2;
@@ -165,7 +164,30 @@ static jboolean android_net_utils_runDhcp(JNIEnv* env, jobject clazz, jstring if
    env->ReleaseStringUTFChars(ifname, nameStr);
    if (result == 0 && dhcpInfoInternalFieldIds.dhcpInfoInternalClass != NULL) {
        env->SetObjectField(info, dhcpInfoInternalFieldIds.ipaddress, env->NewStringUTF(ipaddr));
        env->SetObjectField(info, dhcpInfoInternalFieldIds.gateway, env->NewStringUTF(gateway));

        // set the gateway
        jclass cls = env->FindClass("java/net/InetAddress");
        jmethodID method = env->GetStaticMethodID(cls, "getByName",
                "(Ljava/lang/String;)Ljava/net/InetAddress;");
        jvalue args[1];
        args[0].l = env->NewStringUTF(gateway);
        jobject inetAddressObject = env->CallStaticObjectMethodA(cls, method, args);

        if (!env->ExceptionOccurred()) {
            cls = env->FindClass("android/net/RouteInfo");
            method = env->GetMethodID(cls, "<init>", "(Ljava/net/InetAddress;)V");
            args[0].l = inetAddressObject;
            jobject routeInfoObject = env->NewObjectA(cls, method, args);

            cls = env->FindClass("android/net/DhcpInfoInternal");
            method = env->GetMethodID(cls, "addRoute", "(Landroid/net/RouteInfo;)V");
            args[0].l = routeInfoObject;
            env->CallVoidMethodA(info, method, args);
        } else {
            // if we have an exception (host not found perhaps), just don't add the route
            env->ExceptionClear();
        }

        env->SetIntField(info, dhcpInfoInternalFieldIds.prefixLength, prefixLength);
        env->SetObjectField(info, dhcpInfoInternalFieldIds.dns1, env->NewStringUTF(dns1));
        env->SetObjectField(info, dhcpInfoInternalFieldIds.dns2, env->NewStringUTF(dns2));
@@ -233,7 +255,6 @@ int register_android_net_NetworkUtils(JNIEnv* env)
    if (dhcpInfoInternalFieldIds.dhcpInfoInternalClass != NULL) {
        dhcpInfoInternalFieldIds.constructorId = env->GetMethodID(dhcpInfoInternalFieldIds.dhcpInfoInternalClass, "<init>", "()V");
        dhcpInfoInternalFieldIds.ipaddress = env->GetFieldID(dhcpInfoInternalFieldIds.dhcpInfoInternalClass, "ipAddress", "Ljava/lang/String;");
        dhcpInfoInternalFieldIds.gateway = env->GetFieldID(dhcpInfoInternalFieldIds.dhcpInfoInternalClass, "gateway", "Ljava/lang/String;");
        dhcpInfoInternalFieldIds.prefixLength = env->GetFieldID(dhcpInfoInternalFieldIds.dhcpInfoInternalClass, "prefixLength", "I");
        dhcpInfoInternalFieldIds.dns1 = env->GetFieldID(dhcpInfoInternalFieldIds.dhcpInfoInternalClass, "dns1", "Ljava/lang/String;");
        dhcpInfoInternalFieldIds.dns2 = env->GetFieldID(dhcpInfoInternalFieldIds.dhcpInfoInternalClass, "dns2", "Ljava/lang/String;");
Loading