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