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