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; 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.util.Log; 26 27 import com.android.internal.telephony.DataConnection.FailCause; 28 29 import java.net.Inet4Address; 30 import java.net.InetAddress; 31 import java.net.UnknownHostException; 32 33 /** 34 * This is RIL_Data_Call_Response_v5 from ril.h 35 * TODO: Rename to DataCallResponse. 36 */ 37 public class DataCallState { 38 private final boolean DBG = true; 39 private final String LOG_TAG = "GSM"; 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 52 /** 53 * Class returned by onSetupConnectionCompleted. 54 */ 55 public enum SetupResult { 56 SUCCESS, 57 ERR_BadCommand, 58 ERR_UnacceptableParameter, 59 ERR_GetLastErrorFromRil, 60 ERR_Stale, 61 ERR_RilError; 62 63 public FailCause mFailCause; 64 65 SetupResult() { 66 mFailCause = FailCause.fromInt(0); 67 } 68 69 @Override 70 public String toString() { 71 return name() + " SetupResult.mFailCause=" + mFailCause; 72 } 73 } 74 75 @Override 76 public String toString() { 77 StringBuffer sb = new StringBuffer(); 78 sb.append("DataCallState: {") 79 .append("version=").append(version) 80 .append(" status=").append(status) 81 .append(" retry=").append(suggestedRetryTime) 82 .append(" cid=").append(cid) 83 .append(" active=").append(active) 84 .append(" type=").append(type) 85 .append("' ifname='").append(ifname); 86 sb.append("' addresses=["); 87 for (String addr : addresses) { 88 sb.append(addr); 89 sb.append(","); 90 } 91 if (addresses.length > 0) sb.deleteCharAt(sb.length()-1); 92 sb.append("] dnses=["); 93 for (String addr : dnses) { 94 sb.append(addr); 95 sb.append(","); 96 } 97 if (dnses.length > 0) sb.deleteCharAt(sb.length()-1); 98 sb.append("] gateways=["); 99 for (String addr : gateways) { 100 sb.append(addr); 101 sb.append(","); 102 } 103 if (gateways.length > 0) sb.deleteCharAt(sb.length()-1); 104 sb.append("]}"); 105 return sb.toString(); 106 } 107 108 public SetupResult setLinkProperties(LinkProperties linkProperties, 109 boolean okToUseSystemPropertyDns) { 110 SetupResult result; 111 112 // Start with clean network properties and if we have 113 // a failure we'll clear again at the bottom of this code. 114 if (linkProperties == null) 115 linkProperties = new LinkProperties(); 116 else 117 linkProperties.clear(); 118 119 if (status == FailCause.NONE.getErrorCode()) { 120 String propertyPrefix = "net." + ifname + "."; 121 122 try { 123 // set interface name 124 linkProperties.setInterfaceName(ifname); 125 126 // set link addresses 127 if (addresses != null && addresses.length > 0) { 128 for (String addr : addresses) { 129 LinkAddress la; 130 int addrPrefixLen; 131 132 String [] ap = addr.split("/"); 133 if (ap.length == 2) { 134 addr = ap[0]; 135 addrPrefixLen = Integer.parseInt(ap[1]); 136 } else { 137 addrPrefixLen = 0; 138 } 139 InetAddress ia; 140 try { 141 ia = NetworkUtils.numericToInetAddress(addr); 142 } catch (IllegalArgumentException e) { 143 throw new UnknownHostException("Non-numeric ip addr=" + addr); 144 } 145 if (! ia.isAnyLocalAddress()) { 146 if (addrPrefixLen == 0) { 147 // Assume point to point 148 addrPrefixLen = (ia instanceof Inet4Address) ? 32 : 128; 149 } 150 if (DBG) Log.d(LOG_TAG, "addr/pl=" + addr + "/" + addrPrefixLen); 151 la = new LinkAddress(ia, addrPrefixLen); 152 linkProperties.addLinkAddress(la); 153 } 154 } 155 } else { 156 throw new UnknownHostException("no address for ifname=" + ifname); 157 } 158 159 // set dns servers 160 if (dnses != null && dnses.length > 0) { 161 for (String addr : dnses) { 162 InetAddress ia; 163 try { 164 ia = NetworkUtils.numericToInetAddress(addr); 165 } catch (IllegalArgumentException e) { 166 throw new UnknownHostException("Non-numeric dns addr=" + addr); 167 } 168 if (! ia.isAnyLocalAddress()) { 169 linkProperties.addDns(ia); 170 } 171 } 172 } else if (okToUseSystemPropertyDns){ 173 String dnsServers[] = new String[2]; 174 dnsServers[0] = SystemProperties.get(propertyPrefix + "dns1"); 175 dnsServers[1] = SystemProperties.get(propertyPrefix + "dns2"); 176 for (String dnsAddr : dnsServers) { 177 InetAddress ia; 178 try { 179 ia = NetworkUtils.numericToInetAddress(dnsAddr); 180 } catch (IllegalArgumentException e) { 181 throw new UnknownHostException("Non-numeric dns addr=" + dnsAddr); 182 } 183 if (! ia.isAnyLocalAddress()) { 184 linkProperties.addDns(ia); 185 } 186 } 187 } else { 188 throw new UnknownHostException("Empty dns response and no system default dns"); 189 } 190 191 // set gateways 192 if ((gateways == null) || (gateways.length == 0)) { 193 String sysGateways = SystemProperties.get(propertyPrefix + "gw"); 194 if (sysGateways != null) { 195 gateways = sysGateways.split(" "); 196 } else { 197 gateways = new String[0]; 198 } 199 } 200 for (String addr : gateways) { 201 InetAddress ia; 202 try { 203 ia = NetworkUtils.numericToInetAddress(addr); 204 } catch (IllegalArgumentException e) { 205 throw new UnknownHostException("Non-numeric gateway addr=" + addr); 206 } 207 if (! ia.isAnyLocalAddress()) { 208 linkProperties.addRoute(new RouteInfo(ia)); 209 } 210 } 211 212 result = SetupResult.SUCCESS; 213 } catch (UnknownHostException e) { 214 Log.d(LOG_TAG, "setLinkProperties: UnknownHostException " + e); 215 e.printStackTrace(); 216 result = SetupResult.ERR_UnacceptableParameter; 217 } 218 } else { 219 if (version < 4) { 220 result = SetupResult.ERR_GetLastErrorFromRil; 221 } else { 222 result = SetupResult.ERR_RilError; 223 } 224 } 225 226 // An error occurred so clear properties 227 if (result != SetupResult.SUCCESS) { 228 if(DBG) { 229 Log.d(LOG_TAG, "setLinkProperties: error clearing LinkProperties " + 230 "status=" + status + " result=" + result); 231 } 232 linkProperties.clear(); 233 } 234 235 return result; 236 } 237 } 238