Home | History | Annotate | Download | only in dataconnection
      1 /*
      2  * Copyright (C) 2009 Qualcomm Innovation Center, Inc.  All Rights Reserved.
      3  * Copyright (C) 2009 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package com.android.internal.telephony.dataconnection;
     19 
     20 import android.net.LinkAddress;
     21 import android.net.LinkProperties;
     22 import android.net.NetworkUtils;
     23 import android.net.RouteInfo;
     24 import android.os.SystemProperties;
     25 import android.telephony.Rlog;
     26 
     27 import com.android.internal.telephony.PhoneConstants;
     28 import com.android.internal.telephony.dataconnection.DcFailCause;
     29 
     30 import java.net.Inet4Address;
     31 import java.net.InetAddress;
     32 import java.net.UnknownHostException;
     33 
     34 /**
     35  * This is RIL_Data_Call_Response_v5 from ril.h
     36  */
     37 public class DataCallResponse {
     38     private final boolean DBG = true;
     39     private final String LOG_TAG = "DataCallResponse";
     40 
     41     public int version = 0;
     42     public int status = 0;
     43     public int cid = 0;
     44     public int active = 0;
     45     public String type = "";
     46     public String ifname = "";
     47     public String [] addresses = new String[0];
     48     public String [] dnses = new String[0];
     49     public String[] gateways = new String[0];
     50     public int suggestedRetryTime = -1;
     51     public String [] pcscf = new String[0];
     52     public int mtu = PhoneConstants.UNSET_MTU;
     53 
     54     /**
     55      * Class returned by onSetupConnectionCompleted.
     56      */
     57     public enum SetupResult {
     58         SUCCESS,
     59         ERR_BadCommand,
     60         ERR_UnacceptableParameter,
     61         ERR_GetLastErrorFromRil,
     62         ERR_Stale,
     63         ERR_RilError;
     64 
     65         public DcFailCause mFailCause;
     66 
     67         SetupResult() {
     68             mFailCause = DcFailCause.fromInt(0);
     69         }
     70 
     71         @Override
     72         public String toString() {
     73             return name() + "  SetupResult.mFailCause=" + mFailCause;
     74         }
     75     }
     76 
     77     @Override
     78     public String toString() {
     79         StringBuffer sb = new StringBuffer();
     80         sb.append("DataCallResponse: {")
     81            .append("version=").append(version)
     82            .append(" status=").append(status)
     83            .append(" retry=").append(suggestedRetryTime)
     84            .append(" cid=").append(cid)
     85            .append(" active=").append(active)
     86            .append(" type=").append(type)
     87            .append(" ifname=").append(ifname)
     88            .append(" mtu=").append(mtu)
     89            .append(" addresses=[");
     90         for (String addr : addresses) {
     91             sb.append(addr);
     92             sb.append(",");
     93         }
     94         if (addresses.length > 0) sb.deleteCharAt(sb.length()-1);
     95         sb.append("] dnses=[");
     96         for (String addr : dnses) {
     97             sb.append(addr);
     98             sb.append(",");
     99         }
    100         if (dnses.length > 0) sb.deleteCharAt(sb.length()-1);
    101         sb.append("] gateways=[");
    102         for (String addr : gateways) {
    103             sb.append(addr);
    104             sb.append(",");
    105         }
    106         if (gateways.length > 0) sb.deleteCharAt(sb.length()-1);
    107         sb.append("] pcscf=[");
    108         for (String addr : pcscf) {
    109             sb.append(addr);
    110             sb.append(",");
    111         }
    112         if (pcscf.length > 0) sb.deleteCharAt(sb.length()-1);
    113         sb.append("]}");
    114         return sb.toString();
    115     }
    116 
    117     public SetupResult setLinkProperties(LinkProperties linkProperties,
    118             boolean okToUseSystemPropertyDns) {
    119         SetupResult result;
    120 
    121         // Start with clean network properties and if we have
    122         // a failure we'll clear again at the bottom of this code.
    123         if (linkProperties == null)
    124             linkProperties = new LinkProperties();
    125         else
    126             linkProperties.clear();
    127 
    128         if (status == DcFailCause.NONE.getErrorCode()) {
    129             String propertyPrefix = "net." + ifname + ".";
    130 
    131             try {
    132                 // set interface name
    133                 linkProperties.setInterfaceName(ifname);
    134 
    135                 // set link addresses
    136                 if (addresses != null && addresses.length > 0) {
    137                     for (String addr : addresses) {
    138                         addr = addr.trim();
    139                         if (addr.isEmpty()) continue;
    140                         LinkAddress la;
    141                         int addrPrefixLen;
    142 
    143                         String [] ap = addr.split("/");
    144                         if (ap.length == 2) {
    145                             addr = ap[0];
    146                             addrPrefixLen = Integer.parseInt(ap[1]);
    147                         } else {
    148                             addrPrefixLen = 0;
    149                         }
    150                         InetAddress ia;
    151                         try {
    152                             ia = NetworkUtils.numericToInetAddress(addr);
    153                         } catch (IllegalArgumentException e) {
    154                             throw new UnknownHostException("Non-numeric ip addr=" + addr);
    155                         }
    156                         if (! ia.isAnyLocalAddress()) {
    157                             if (addrPrefixLen == 0) {
    158                                 // Assume point to point
    159                                 addrPrefixLen = (ia instanceof Inet4Address) ? 32 : 128;
    160                             }
    161                             if (DBG) Rlog.d(LOG_TAG, "addr/pl=" + addr + "/" + addrPrefixLen);
    162                             la = new LinkAddress(ia, addrPrefixLen);
    163                             linkProperties.addLinkAddress(la);
    164                         }
    165                     }
    166                 } else {
    167                     throw new UnknownHostException("no address for ifname=" + ifname);
    168                 }
    169 
    170                 // set dns servers
    171                 if (dnses != null && dnses.length > 0) {
    172                     for (String addr : dnses) {
    173                         addr = addr.trim();
    174                         if (addr.isEmpty()) continue;
    175                         InetAddress ia;
    176                         try {
    177                             ia = NetworkUtils.numericToInetAddress(addr);
    178                         } catch (IllegalArgumentException e) {
    179                             throw new UnknownHostException("Non-numeric dns addr=" + addr);
    180                         }
    181                         if (! ia.isAnyLocalAddress()) {
    182                             linkProperties.addDnsServer(ia);
    183                         }
    184                     }
    185                 } else if (okToUseSystemPropertyDns){
    186                     String dnsServers[] = new String[2];
    187                     dnsServers[0] = SystemProperties.get(propertyPrefix + "dns1");
    188                     dnsServers[1] = SystemProperties.get(propertyPrefix + "dns2");
    189                     for (String dnsAddr : dnsServers) {
    190                         dnsAddr = dnsAddr.trim();
    191                         if (dnsAddr.isEmpty()) continue;
    192                         InetAddress ia;
    193                         try {
    194                             ia = NetworkUtils.numericToInetAddress(dnsAddr);
    195                         } catch (IllegalArgumentException e) {
    196                             throw new UnknownHostException("Non-numeric dns addr=" + dnsAddr);
    197                         }
    198                         if (! ia.isAnyLocalAddress()) {
    199                             linkProperties.addDnsServer(ia);
    200                         }
    201                     }
    202                 } else {
    203                     throw new UnknownHostException("Empty dns response and no system default dns");
    204                 }
    205 
    206                 // set gateways
    207                 if ((gateways == null) || (gateways.length == 0)) {
    208                     String sysGateways = SystemProperties.get(propertyPrefix + "gw");
    209                     if (sysGateways != null) {
    210                         gateways = sysGateways.split(" ");
    211                     } else {
    212                         gateways = new String[0];
    213                     }
    214                 }
    215                 for (String addr : gateways) {
    216                     addr = addr.trim();
    217                     if (addr.isEmpty()) continue;
    218                     InetAddress ia;
    219                     try {
    220                         ia = NetworkUtils.numericToInetAddress(addr);
    221                     } catch (IllegalArgumentException e) {
    222                         throw new UnknownHostException("Non-numeric gateway addr=" + addr);
    223                     }
    224                     // Allow 0.0.0.0 or :: as a gateway; this indicates a point-to-point interface.
    225                     linkProperties.addRoute(new RouteInfo(ia));
    226                 }
    227 
    228                 // set interface MTU
    229                 // this may clobber the setting read from the APN db, but that's ok
    230                 linkProperties.setMtu(mtu);
    231 
    232                 result = SetupResult.SUCCESS;
    233             } catch (UnknownHostException e) {
    234                 Rlog.d(LOG_TAG, "setLinkProperties: UnknownHostException " + e);
    235                 e.printStackTrace();
    236                 result = SetupResult.ERR_UnacceptableParameter;
    237             }
    238         } else {
    239             if (version < 4) {
    240                 result = SetupResult.ERR_GetLastErrorFromRil;
    241             } else {
    242                 result = SetupResult.ERR_RilError;
    243             }
    244         }
    245 
    246         // An error occurred so clear properties
    247         if (result != SetupResult.SUCCESS) {
    248             if(DBG) {
    249                 Rlog.d(LOG_TAG, "setLinkProperties: error clearing LinkProperties " +
    250                         "status=" + status + " result=" + result);
    251             }
    252             linkProperties.clear();
    253         }
    254 
    255         return result;
    256     }
    257 }
    258