VpnConfig.java revision 05542603dd4f1e0ea47a3dca01de3999a9a329a9
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.net;
18
19import android.app.PendingIntent;
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
24import android.content.pm.PackageManager.NameNotFoundException;
25import android.content.pm.ResolveInfo;
26import android.content.res.Resources;
27import android.net.LinkAddress;
28import android.net.RouteInfo;
29import android.os.Parcel;
30import android.os.Parcelable;
31
32import java.net.Inet4Address;
33import java.net.InetAddress;
34import java.util.ArrayList;
35import java.util.List;
36
37/**
38 * A simple container used to carry information in VpnBuilder, VpnDialogs,
39 * and com.android.server.connectivity.Vpn. Internal use only.
40 *
41 * @hide
42 */
43public class VpnConfig implements Parcelable {
44
45    public static final String SERVICE_INTERFACE = "android.net.VpnService";
46
47    public static final String DIALOGS_PACKAGE = "com.android.vpndialogs";
48
49    public static final String LEGACY_VPN = "[Legacy VPN]";
50
51    public static Intent getIntentForConfirmation() {
52        Intent intent = new Intent();
53        ComponentName componentName = ComponentName.unflattenFromString(
54                Resources.getSystem().getString(
55                        com.android.internal.R.string.config_customVpnConfirmDialogComponent));
56        intent.setClassName(componentName.getPackageName(), componentName.getClassName());
57        return intent;
58    }
59
60    public static CharSequence getVpnLabel(Context context, String packageName)
61            throws NameNotFoundException {
62        PackageManager pm = context.getPackageManager();
63        Intent intent = new Intent(SERVICE_INTERFACE);
64        intent.setPackage(packageName);
65        List<ResolveInfo> services = pm.queryIntentServices(intent, 0 /* flags */);
66        if (services != null && services.size() == 1) {
67            // This app contains exactly one VPN service. Call loadLabel, which will attempt to
68            // load the service's label, and fall back to the app label if none is present.
69            return services.get(0).loadLabel(pm);
70        } else {
71            return pm.getApplicationInfo(packageName, 0).loadLabel(pm);
72        }
73    }
74
75    public String user;
76    public String interfaze;
77    public String session;
78    public int mtu = -1;
79    public List<LinkAddress> addresses = new ArrayList<LinkAddress>();
80    public List<RouteInfo> routes = new ArrayList<RouteInfo>();
81    public List<String> dnsServers;
82    public List<String> searchDomains;
83    public PendingIntent configureIntent;
84    public long startTime = -1;
85    public boolean legacy;
86    public boolean blocking;
87    public boolean allowBypass;
88    public boolean allowIPv4;
89    public boolean allowIPv6;
90
91    public void updateAllowedFamilies(InetAddress address) {
92        if (address instanceof Inet4Address) {
93            allowIPv4 = true;
94        } else {
95            allowIPv6 = true;
96        }
97    }
98
99    public void addLegacyRoutes(String routesStr) {
100        if (routesStr.trim().equals("")) {
101            return;
102        }
103        String[] routes = routesStr.trim().split(" ");
104        for (String route : routes) {
105            //each route is ip/prefix
106            String[] split = route.split("/");
107            RouteInfo info = new RouteInfo(new LinkAddress
108                    (InetAddress.parseNumericAddress(split[0]), Integer.parseInt(split[1])), null);
109            this.routes.add(info);
110            updateAllowedFamilies(info.getDestination().getAddress());
111        }
112    }
113
114    public void addLegacyAddresses(String addressesStr) {
115        if (addressesStr.trim().equals("")) {
116            return;
117        }
118        String[] addresses = addressesStr.trim().split(" ");
119        for (String address : addresses) {
120            //each address is ip/prefix
121            String[] split = address.split("/");
122            LinkAddress addr = new LinkAddress(InetAddress.parseNumericAddress(split[0]),
123                    Integer.parseInt(split[1]));
124            this.addresses.add(addr);
125            updateAllowedFamilies(addr.getAddress());
126        }
127    }
128
129    @Override
130    public int describeContents() {
131        return 0;
132    }
133
134    @Override
135    public void writeToParcel(Parcel out, int flags) {
136        out.writeString(user);
137        out.writeString(interfaze);
138        out.writeString(session);
139        out.writeInt(mtu);
140        out.writeTypedList(addresses);
141        out.writeTypedList(routes);
142        out.writeStringList(dnsServers);
143        out.writeStringList(searchDomains);
144        out.writeParcelable(configureIntent, flags);
145        out.writeLong(startTime);
146        out.writeInt(legacy ? 1 : 0);
147        out.writeInt(blocking ? 1 : 0);
148        out.writeInt(allowBypass ? 1 : 0);
149        out.writeInt(allowIPv4 ? 1 : 0);
150        out.writeInt(allowIPv6 ? 1 : 0);
151    }
152
153    public static final Parcelable.Creator<VpnConfig> CREATOR =
154            new Parcelable.Creator<VpnConfig>() {
155        @Override
156        public VpnConfig createFromParcel(Parcel in) {
157            VpnConfig config = new VpnConfig();
158            config.user = in.readString();
159            config.interfaze = in.readString();
160            config.session = in.readString();
161            config.mtu = in.readInt();
162            in.readTypedList(config.addresses, LinkAddress.CREATOR);
163            in.readTypedList(config.routes, RouteInfo.CREATOR);
164            config.dnsServers = in.createStringArrayList();
165            config.searchDomains = in.createStringArrayList();
166            config.configureIntent = in.readParcelable(null);
167            config.startTime = in.readLong();
168            config.legacy = in.readInt() != 0;
169            config.blocking = in.readInt() != 0;
170            config.allowBypass = in.readInt() != 0;
171            config.allowIPv4 = in.readInt() != 0;
172            config.allowIPv6 = in.readInt() != 0;
173            return config;
174        }
175
176        @Override
177        public VpnConfig[] newArray(int size) {
178            return new VpnConfig[size];
179        }
180    };
181}
182