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

Commit 8a6f9c11 authored by Jack Yu's avatar Jack Yu Committed by Android (Google) Code Review
Browse files

Merge "Combined setLinkProperties in DataConnection and DataResponse"

parents a2de1c0e 86632deb
Loading
Loading
Loading
Loading
+17 −178
Original line number Diff line number Diff line
@@ -17,18 +17,9 @@

package com.android.internal.telephony.dataconnection;

import android.net.LinkAddress;
import android.net.LinkProperties;
import android.net.NetworkUtils;
import android.net.RouteInfo;
import android.os.SystemProperties;
import android.telephony.Rlog;
import android.text.TextUtils;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * This is RIL_Data_Call_Response_v5 from ril.h
 */
@@ -44,34 +35,10 @@ public class DataCallResponse {
    public final String ifname;
    public final String [] addresses;
    public final String [] dnses;
    // TODO: Change this to final if possible.
    public String[] gateways;
    public final String[] gateways;
    public final String [] pcscf;
    public final int mtu;

    /**
     * Class returned by onSetupConnectionCompleted.
     */
    public enum SetupResult {
        SUCCESS,
        ERR_BadCommand,
        ERR_UnacceptableParameter,
        ERR_GetLastErrorFromRil,
        ERR_Stale,
        ERR_RilError;

        public DcFailCause mFailCause;

        SetupResult() {
            mFailCause = DcFailCause.fromInt(0);
        }

        @Override
        public String toString() {
            return name() + "  SetupResult.mFailCause=" + mFailCause;
        }
    }

    public DataCallResponse(int status, int suggestedRetryTime, int cid, int active, String type,
                            String ifname, String addresses, String dnses, String gateways,
                            String pcscf, int mtu) {
@@ -86,7 +53,22 @@ public class DataCallResponse {
        }
        this.addresses = TextUtils.isEmpty(addresses) ? new String[0] : addresses.split(" ");
        this.dnses = TextUtils.isEmpty(dnses) ? new String[0] : dnses.split(" ");
        this.gateways = TextUtils.isEmpty(gateways) ? new String[0] : gateways.split(" ");

        String[] myGateways = TextUtils.isEmpty(gateways)
                ? new String[0] : gateways.split(" ");

        // set gateways
        if (myGateways.length == 0) {
            String propertyPrefix = "net." + this.ifname + ".";
            String sysGateways = SystemProperties.get(propertyPrefix + "gw");
            if (sysGateways != null) {
                myGateways = sysGateways.split(" ");
            } else {
                myGateways = new String[0];
            }
        }
        this.gateways = myGateways;

        this.pcscf = TextUtils.isEmpty(pcscf) ? new String[0] : pcscf.split(" ");
        this.mtu = mtu;
    }
@@ -129,147 +111,4 @@ public class DataCallResponse {
        sb.append("]}");
        return sb.toString();
    }

    public SetupResult setLinkProperties(LinkProperties linkProperties,
            boolean okToUseSystemPropertyDns) {
        SetupResult result;

        // Start with clean network properties and if we have
        // a failure we'll clear again at the bottom of this code.
        if (linkProperties == null)
            linkProperties = new LinkProperties();
        else
            linkProperties.clear();

        if (status == DcFailCause.NONE.getErrorCode()) {
            String propertyPrefix = "net." + ifname + ".";

            try {
                // set interface name
                linkProperties.setInterfaceName(ifname);

                // set link addresses
                if (addresses != null && addresses.length > 0) {
                    for (String addr : addresses) {
                        addr = addr.trim();
                        if (addr.isEmpty()) continue;
                        LinkAddress la;
                        int addrPrefixLen;

                        String [] ap = addr.split("/");
                        if (ap.length == 2) {
                            addr = ap[0];
                            addrPrefixLen = Integer.parseInt(ap[1]);
                        } else {
                            addrPrefixLen = 0;
                        }
                        InetAddress ia;
                        try {
                            ia = NetworkUtils.numericToInetAddress(addr);
                        } catch (IllegalArgumentException e) {
                            throw new UnknownHostException("Non-numeric ip addr=" + addr);
                        }
                        if (! ia.isAnyLocalAddress()) {
                            if (addrPrefixLen == 0) {
                                // Assume point to point
                                addrPrefixLen = (ia instanceof Inet4Address) ? 32 : 128;
                            }
                            if (DBG) Rlog.d(LOG_TAG, "addr/pl=" + addr + "/" + addrPrefixLen);
                            try {
                                la = new LinkAddress(ia, addrPrefixLen);
                            } catch (IllegalArgumentException e) {
                                throw new UnknownHostException("Bad parameter for LinkAddress, ia="
                                        + ia.getHostAddress() + "/" + addrPrefixLen);
                            }

                            linkProperties.addLinkAddress(la);
                        }
                    }
                } else {
                    throw new UnknownHostException("no address for ifname=" + ifname);
                }

                // set dns servers
                if (dnses != null && dnses.length > 0) {
                    for (String addr : dnses) {
                        addr = addr.trim();
                        if (addr.isEmpty()) continue;
                        InetAddress ia;
                        try {
                            ia = NetworkUtils.numericToInetAddress(addr);
                        } catch (IllegalArgumentException e) {
                            throw new UnknownHostException("Non-numeric dns addr=" + addr);
                        }
                        if (! ia.isAnyLocalAddress()) {
                            linkProperties.addDnsServer(ia);
                        }
                    }
                } else if (okToUseSystemPropertyDns){
                    String dnsServers[] = new String[2];
                    dnsServers[0] = SystemProperties.get(propertyPrefix + "dns1");
                    dnsServers[1] = SystemProperties.get(propertyPrefix + "dns2");
                    for (String dnsAddr : dnsServers) {
                        dnsAddr = dnsAddr.trim();
                        if (dnsAddr.isEmpty()) continue;
                        InetAddress ia;
                        try {
                            ia = NetworkUtils.numericToInetAddress(dnsAddr);
                        } catch (IllegalArgumentException e) {
                            throw new UnknownHostException("Non-numeric dns addr=" + dnsAddr);
                        }
                        if (! ia.isAnyLocalAddress()) {
                            linkProperties.addDnsServer(ia);
                        }
                    }
                } else {
                    throw new UnknownHostException("Empty dns response and no system default dns");
                }

                // set gateways
                if ((gateways == null) || (gateways.length == 0)) {
                    String sysGateways = SystemProperties.get(propertyPrefix + "gw");
                    if (sysGateways != null) {
                        gateways = sysGateways.split(" ");
                    } else {
                        gateways = new String[0];
                    }
                }
                for (String addr : gateways) {
                    addr = addr.trim();
                    if (addr.isEmpty()) continue;
                    InetAddress ia;
                    try {
                        ia = NetworkUtils.numericToInetAddress(addr);
                    } catch (IllegalArgumentException e) {
                        throw new UnknownHostException("Non-numeric gateway addr=" + addr);
                    }
                    // Allow 0.0.0.0 or :: as a gateway; this indicates a point-to-point interface.
                    linkProperties.addRoute(new RouteInfo(ia));
                }

                // set interface MTU
                // this may clobber the setting read from the APN db, but that's ok
                linkProperties.setMtu(mtu);

                result = SetupResult.SUCCESS;
            } catch (UnknownHostException e) {
                Rlog.d(LOG_TAG, "setLinkProperties: UnknownHostException " + e);
                e.printStackTrace();
                result = SetupResult.ERR_UnacceptableParameter;
            }
        } else {
            result = SetupResult.ERR_RilError;
        }

        // An error occurred so clear properties
        if (result != SetupResult.SUCCESS) {
            if(DBG) {
                Rlog.d(LOG_TAG, "setLinkProperties: error clearing LinkProperties " +
                        "status=" + status + " result=" + result);
            }
            linkProperties.clear();
        }

        return result;
    }
}
+166 −17
Original line number Diff line number Diff line
@@ -19,12 +19,15 @@ package com.android.internal.telephony.dataconnection;
import android.app.PendingIntent;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.LinkAddress;
import android.net.LinkProperties;
import android.net.NetworkAgent;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkMisc;
import android.net.NetworkUtils;
import android.net.ProxyInfo;
import android.net.RouteInfo;
import android.net.StringNetworkSpecifier;
import android.os.AsyncResult;
import android.os.Looper;
@@ -59,7 +62,9 @@ import com.android.internal.util.StateMachine;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
@@ -283,7 +288,7 @@ public class DataConnection extends StateMachine {
    }

    public static class UpdateLinkPropertyResult {
        public DataCallResponse.SetupResult setupResult = DataCallResponse.SetupResult.SUCCESS;
        public SetupResult setupResult = SetupResult.SUCCESS;
        public LinkProperties oldLp;
        public LinkProperties newLp;
        public UpdateLinkPropertyResult(LinkProperties curLp) {
@@ -292,6 +297,29 @@ public class DataConnection extends StateMachine {
        }
    }

    /**
     * Class returned by onSetupConnectionCompleted.
     */
    public enum SetupResult {
        SUCCESS,
        ERR_BadCommand,
        ERR_UnacceptableParameter,
        ERR_GetLastErrorFromRil,
        ERR_Stale,
        ERR_RilError;

        public DcFailCause mFailCause;

        SetupResult() {
            mFailCause = DcFailCause.fromInt(0);
        }

        @Override
        public String toString() {
            return name() + "  SetupResult.mFailCause=" + mFailCause;
        }
    }

    public boolean isIpv4Connected() {
        boolean ret = false;
        Collection <InetAddress> addresses = mLinkProperties.getAddresses();
@@ -331,12 +359,12 @@ public class DataConnection extends StateMachine {

        if (newState == null) return result;

        DataCallResponse.SetupResult setupResult;
        SetupResult setupResult;
        result.newLp = new LinkProperties();

        // set link properties based on data call response
        result.setupResult = setLinkProperties(newState, result.newLp);
        if (result.setupResult != DataCallResponse.SetupResult.SUCCESS) {
        if (result.setupResult != SetupResult.SUCCESS) {
            if (DBG) log("updateLinkProperty failed : " + result.setupResult);
            return result;
        }
@@ -665,16 +693,16 @@ public class DataConnection extends StateMachine {
     * @param ar is the result
     * @return SetupResult.
     */
    private DataCallResponse.SetupResult onSetupConnectionCompleted(AsyncResult ar) {
    private SetupResult onSetupConnectionCompleted(AsyncResult ar) {
        DataCallResponse response = (DataCallResponse) ar.result;
        ConnectionParams cp = (ConnectionParams) ar.userObj;
        DataCallResponse.SetupResult result;
        SetupResult result;

        if (cp.mTag != mTag) {
            if (DBG) {
                log("onSetupConnectionCompleted stale cp.tag=" + cp.mTag + ", mtag=" + mTag);
            }
            result = DataCallResponse.SetupResult.ERR_Stale;
            result = SetupResult.ERR_Stale;
        } else if (ar.exception != null) {
            if (DBG) {
                log("onSetupConnectionCompleted failed, ar.exception=" + ar.exception +
@@ -684,14 +712,14 @@ public class DataConnection extends StateMachine {
            if (ar.exception instanceof CommandException
                    && ((CommandException) (ar.exception)).getCommandError()
                    == CommandException.Error.RADIO_NOT_AVAILABLE) {
                result = DataCallResponse.SetupResult.ERR_BadCommand;
                result = SetupResult.ERR_BadCommand;
                result.mFailCause = DcFailCause.RADIO_NOT_AVAILABLE;
            } else {
                result = DataCallResponse.SetupResult.ERR_RilError;
                result = SetupResult.ERR_RilError;
                result.mFailCause = DcFailCause.fromInt(response.status);
            }
        } else if (response.status != 0) {
            result = DataCallResponse.SetupResult.ERR_RilError;
            result = SetupResult.ERR_RilError;
            result.mFailCause = DcFailCause.fromInt(response.status);
        } else {
            if (DBG) log("onSetupConnectionCompleted received successful DataCallResponse");
@@ -995,18 +1023,139 @@ public class DataConnection extends StateMachine {
        return InetAddress.isNumeric(address);
    }

    private DataCallResponse.SetupResult setLinkProperties(DataCallResponse response,
            LinkProperties lp) {
    private SetupResult setLinkProperties(DataCallResponse response,
            LinkProperties linkProperties) {
        // Check if system property dns usable
        boolean okToUseSystemPropertyDns = false;
        String propertyPrefix = "net." + response.ifname + ".";
        String dnsServers[] = new String[2];
        dnsServers[0] = SystemProperties.get(propertyPrefix + "dns1");
        dnsServers[1] = SystemProperties.get(propertyPrefix + "dns2");
        okToUseSystemPropertyDns = isDnsOk(dnsServers);
        boolean okToUseSystemPropertyDns = isDnsOk(dnsServers);

        SetupResult result;

        // Start with clean network properties and if we have
        // a failure we'll clear again at the bottom of this code.
        linkProperties.clear();

        if (response.status == DcFailCause.NONE.getErrorCode()) {
            try {
                // set interface name
                linkProperties.setInterfaceName(response.ifname);

                // set link addresses
                if (response.addresses != null && response.addresses.length > 0) {
                    for (String addr : response.addresses) {
                        addr = addr.trim();
                        if (addr.isEmpty()) continue;
                        LinkAddress la;
                        int addrPrefixLen;

                        String [] ap = addr.split("/");
                        if (ap.length == 2) {
                            addr = ap[0];
                            addrPrefixLen = Integer.parseInt(ap[1]);
                        } else {
                            addrPrefixLen = 0;
                        }
                        InetAddress ia;
                        try {
                            ia = NetworkUtils.numericToInetAddress(addr);
                        } catch (IllegalArgumentException e) {
                            throw new UnknownHostException("Non-numeric ip addr=" + addr);
                        }
                        if (!ia.isAnyLocalAddress()) {
                            if (addrPrefixLen == 0) {
                                // Assume point to point
                                addrPrefixLen = (ia instanceof Inet4Address) ? 32 : 128;
                            }
                            if (DBG) log("addr/pl=" + addr + "/" + addrPrefixLen);
                            try {
                                la = new LinkAddress(ia, addrPrefixLen);
                            } catch (IllegalArgumentException e) {
                                throw new UnknownHostException("Bad parameter for LinkAddress, ia="
                                        + ia.getHostAddress() + "/" + addrPrefixLen);
                            }

        // set link properties based on data call response
        return response.setLinkProperties(lp, okToUseSystemPropertyDns);
                            linkProperties.addLinkAddress(la);
                        }
                    }
                } else {
                    throw new UnknownHostException("no address for ifname=" + response.ifname);
                }

                // set dns servers
                if (response.dnses != null && response.dnses.length > 0) {
                    for (String addr : response.dnses) {
                        addr = addr.trim();
                        if (addr.isEmpty()) continue;
                        InetAddress ia;
                        try {
                            ia = NetworkUtils.numericToInetAddress(addr);
                        } catch (IllegalArgumentException e) {
                            throw new UnknownHostException("Non-numeric dns addr=" + addr);
                        }
                        if (!ia.isAnyLocalAddress()) {
                            linkProperties.addDnsServer(ia);
                        }
                    }
                } else if (okToUseSystemPropertyDns) {
                    dnsServers[0] = SystemProperties.get(propertyPrefix + "dns1");
                    dnsServers[1] = SystemProperties.get(propertyPrefix + "dns2");
                    for (String dnsAddr : dnsServers) {
                        dnsAddr = dnsAddr.trim();
                        if (dnsAddr.isEmpty()) continue;
                        InetAddress ia;
                        try {
                            ia = NetworkUtils.numericToInetAddress(dnsAddr);
                        } catch (IllegalArgumentException e) {
                            throw new UnknownHostException("Non-numeric dns addr=" + dnsAddr);
                        }
                        if (!ia.isAnyLocalAddress()) {
                            linkProperties.addDnsServer(ia);
                        }
                    }
                } else {
                    throw new UnknownHostException("Empty dns response and no system default dns");
                }

                for (String addr : response.gateways) {
                    addr = addr.trim();
                    if (addr.isEmpty()) continue;
                    InetAddress ia;
                    try {
                        ia = NetworkUtils.numericToInetAddress(addr);
                    } catch (IllegalArgumentException e) {
                        throw new UnknownHostException("Non-numeric gateway addr=" + addr);
                    }
                    // Allow 0.0.0.0 or :: as a gateway; this indicates a point-to-point interface.
                    linkProperties.addRoute(new RouteInfo(ia));
                }

                // set interface MTU
                // this may clobber the setting read from the APN db, but that's ok
                linkProperties.setMtu(response.mtu);

                result = SetupResult.SUCCESS;
            } catch (UnknownHostException e) {
                log("setLinkProperties: UnknownHostException " + e);
                e.printStackTrace();
                result = SetupResult.ERR_UnacceptableParameter;
            }
        } else {
            result = SetupResult.ERR_RilError;
        }

        // An error occurred so clear properties
        if (result != SetupResult.SUCCESS) {
            if (DBG) {
                log("setLinkProperties: error clearing LinkProperties status=" + response.status
                        + " result=" + result);
            }
            linkProperties.clear();
        }

        return result;
    }

    /**
@@ -1421,8 +1570,8 @@ public class DataConnection extends StateMachine {
                    ar = (AsyncResult) msg.obj;
                    cp = (ConnectionParams) ar.userObj;

                    DataCallResponse.SetupResult result = onSetupConnectionCompleted(ar);
                    if (result != DataCallResponse.SetupResult.ERR_Stale) {
                    SetupResult result = onSetupConnectionCompleted(ar);
                    if (result != SetupResult.ERR_Stale) {
                        if (mConnectionParams != cp) {
                            loge("DcActivatingState: WEIRD mConnectionsParams:"+ mConnectionParams
                                    + " != cp:" + cp);
+0 −94
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 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 com.android.internal.telephony.dataconnection;

import static com.android.internal.telephony.dataconnection.DcTrackerTest.FAKE_ADDRESS;
import static com.android.internal.telephony.dataconnection.DcTrackerTest.FAKE_DNS;
import static com.android.internal.telephony.dataconnection.DcTrackerTest.FAKE_GATEWAY;
import static com.android.internal.telephony.dataconnection.DcTrackerTest.FAKE_IFNAME;
import static com.android.internal.telephony.dataconnection.DcTrackerTest.FAKE_PCSCF_ADDRESS;

import static org.junit.Assert.assertEquals;

import android.net.LinkProperties;
import android.test.suitebuilder.annotation.SmallTest;

import com.android.internal.telephony.TelephonyTest;
import com.android.internal.telephony.dataconnection.DataCallResponse.SetupResult;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class DataCallResponseTest extends TelephonyTest {

    DataCallResponse mDcResponse;

    @Before
    public void setUp() throws Exception {
        super.setUp(getClass().getSimpleName());
        mDcResponse = new DataCallResponse(0, -1, 1, 2, "IP", FAKE_IFNAME, FAKE_ADDRESS,
                FAKE_DNS, FAKE_GATEWAY, FAKE_PCSCF_ADDRESS, 1440);
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }

    @Test
    @SmallTest
    public void testSetLinkProperties() throws Exception {
        LinkProperties linkProperties = new LinkProperties();
        assertEquals(SetupResult.SUCCESS,
                mDcResponse.setLinkProperties(linkProperties, true));
        logd(linkProperties.toString());
        assertEquals(mDcResponse.ifname, linkProperties.getInterfaceName());
        assertEquals(mDcResponse.addresses.length, linkProperties.getAddresses().size());
        for (int i = 0; i < mDcResponse.addresses.length; ++i) {
            assertEquals(mDcResponse.addresses[i],
                    linkProperties.getLinkAddresses().get(i).getAddress().getHostAddress());
        }

        assertEquals(mDcResponse.dnses.length, linkProperties.getDnsServers().size());
        for (int i = 0; i < mDcResponse.dnses.length; ++i) {
            assertEquals("i = " + i, mDcResponse.dnses[i],
                    linkProperties.getDnsServers().get(i).getHostAddress());
        }

        assertEquals(mDcResponse.gateways.length, linkProperties.getRoutes().size());
        for (int i = 0; i < mDcResponse.gateways.length; ++i) {
            assertEquals("i = " + i, mDcResponse.gateways[i],
                    linkProperties.getRoutes().get(i).getGateway().getHostAddress());
        }

        assertEquals(mDcResponse.mtu, linkProperties.getMtu());
    }

    @Test
    @SmallTest
    public void testSetLinkPropertiesInvalidAddress() throws Exception {

        // 224.224.224.224 is an invalid address.
        mDcResponse = new DataCallResponse(0, -1, 1, 2, "IP", FAKE_IFNAME, "224.224.224.224",
                FAKE_DNS, FAKE_GATEWAY, FAKE_PCSCF_ADDRESS, 1440);

        LinkProperties linkProperties = new LinkProperties();
        assertEquals(SetupResult.ERR_UnacceptableParameter,
                mDcResponse.setLinkProperties(linkProperties, true));
    }
}
 No newline at end of file
+59 −0

File changed.

Preview size limit exceeded, changes collapsed.