VpnConfig.java revision 0784eeab28da094a87437ed454fe3dca01b1f9f2
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; 31import android.os.UserHandle; 32 33import java.net.Inet4Address; 34import java.net.InetAddress; 35import java.util.ArrayList; 36import java.util.List; 37 38/** 39 * A simple container used to carry information in VpnBuilder, VpnDialogs, 40 * and com.android.server.connectivity.Vpn. Internal use only. 41 * 42 * @hide 43 */ 44public class VpnConfig implements Parcelable { 45 46 public static final String SERVICE_INTERFACE = "android.net.VpnService"; 47 48 public static final String DIALOGS_PACKAGE = "com.android.vpndialogs"; 49 50 public static final String LEGACY_VPN = "[Legacy VPN]"; 51 52 public static Intent getIntentForConfirmation() { 53 Intent intent = new Intent(); 54 ComponentName componentName = ComponentName.unflattenFromString( 55 Resources.getSystem().getString( 56 com.android.internal.R.string.config_customVpnConfirmDialogComponent)); 57 intent.setClassName(componentName.getPackageName(), componentName.getClassName()); 58 return intent; 59 } 60 61 /** NOTE: This should only be used for legacy VPN. */ 62 public static PendingIntent getIntentForStatusPanel(Context context) { 63 Intent intent = new Intent(); 64 intent.setClassName(DIALOGS_PACKAGE, DIALOGS_PACKAGE + ".ManageDialog"); 65 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY | 66 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 67 return PendingIntent.getActivityAsUser(context, 0, intent, 0, null, UserHandle.CURRENT); 68 } 69 70 public static CharSequence getVpnLabel(Context context, String packageName) 71 throws NameNotFoundException { 72 PackageManager pm = context.getPackageManager(); 73 Intent intent = new Intent(SERVICE_INTERFACE); 74 intent.setPackage(packageName); 75 List<ResolveInfo> services = pm.queryIntentServices(intent, 0 /* flags */); 76 if (services != null && services.size() == 1) { 77 // This app contains exactly one VPN service. Call loadLabel, which will attempt to 78 // load the service's label, and fall back to the app label if none is present. 79 return services.get(0).loadLabel(pm); 80 } else { 81 return pm.getApplicationInfo(packageName, 0).loadLabel(pm); 82 } 83 } 84 85 public String user; 86 public String interfaze; 87 public String session; 88 public int mtu = -1; 89 public List<LinkAddress> addresses = new ArrayList<LinkAddress>(); 90 public List<RouteInfo> routes = new ArrayList<RouteInfo>(); 91 public List<String> dnsServers; 92 public List<String> searchDomains; 93 public List<String> allowedApplications; 94 public List<String> disallowedApplications; 95 public PendingIntent configureIntent; 96 public long startTime = -1; 97 public boolean legacy; 98 public boolean blocking; 99 public boolean allowBypass; 100 public boolean allowIPv4; 101 public boolean allowIPv6; 102 103 public void updateAllowedFamilies(InetAddress address) { 104 if (address instanceof Inet4Address) { 105 allowIPv4 = true; 106 } else { 107 allowIPv6 = true; 108 } 109 } 110 111 public void addLegacyRoutes(String routesStr) { 112 if (routesStr.trim().equals("")) { 113 return; 114 } 115 String[] routes = routesStr.trim().split(" "); 116 for (String route : routes) { 117 //each route is ip/prefix 118 String[] split = route.split("/"); 119 RouteInfo info = new RouteInfo(new LinkAddress 120 (InetAddress.parseNumericAddress(split[0]), Integer.parseInt(split[1])), null); 121 this.routes.add(info); 122 updateAllowedFamilies(info.getDestination().getAddress()); 123 } 124 } 125 126 public void addLegacyAddresses(String addressesStr) { 127 if (addressesStr.trim().equals("")) { 128 return; 129 } 130 String[] addresses = addressesStr.trim().split(" "); 131 for (String address : addresses) { 132 //each address is ip/prefix 133 String[] split = address.split("/"); 134 LinkAddress addr = new LinkAddress(InetAddress.parseNumericAddress(split[0]), 135 Integer.parseInt(split[1])); 136 this.addresses.add(addr); 137 updateAllowedFamilies(addr.getAddress()); 138 } 139 } 140 141 @Override 142 public int describeContents() { 143 return 0; 144 } 145 146 @Override 147 public void writeToParcel(Parcel out, int flags) { 148 out.writeString(user); 149 out.writeString(interfaze); 150 out.writeString(session); 151 out.writeInt(mtu); 152 out.writeTypedList(addresses); 153 out.writeTypedList(routes); 154 out.writeStringList(dnsServers); 155 out.writeStringList(searchDomains); 156 out.writeStringList(allowedApplications); 157 out.writeStringList(disallowedApplications); 158 out.writeParcelable(configureIntent, flags); 159 out.writeLong(startTime); 160 out.writeInt(legacy ? 1 : 0); 161 out.writeInt(blocking ? 1 : 0); 162 out.writeInt(allowBypass ? 1 : 0); 163 out.writeInt(allowIPv4 ? 1 : 0); 164 out.writeInt(allowIPv6 ? 1 : 0); 165 } 166 167 public static final Parcelable.Creator<VpnConfig> CREATOR = 168 new Parcelable.Creator<VpnConfig>() { 169 @Override 170 public VpnConfig createFromParcel(Parcel in) { 171 VpnConfig config = new VpnConfig(); 172 config.user = in.readString(); 173 config.interfaze = in.readString(); 174 config.session = in.readString(); 175 config.mtu = in.readInt(); 176 in.readTypedList(config.addresses, LinkAddress.CREATOR); 177 in.readTypedList(config.routes, RouteInfo.CREATOR); 178 config.dnsServers = in.createStringArrayList(); 179 config.searchDomains = in.createStringArrayList(); 180 config.allowedApplications = in.createStringArrayList(); 181 config.disallowedApplications = in.createStringArrayList(); 182 config.configureIntent = in.readParcelable(null); 183 config.startTime = in.readLong(); 184 config.legacy = in.readInt() != 0; 185 config.blocking = in.readInt() != 0; 186 config.allowBypass = in.readInt() != 0; 187 config.allowIPv4 = in.readInt() != 0; 188 config.allowIPv6 = in.readInt() != 0; 189 return config; 190 } 191 192 @Override 193 public VpnConfig[] newArray(int size) { 194 return new VpnConfig[size]; 195 } 196 }; 197} 198