Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 1 | /* |
Hung-ying Tyan | d3aba7f | 2009-06-19 19:45:38 +0800 | [diff] [blame] | 2 | * Copyright (C) 2009, The Android Open Source Project |
Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 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 | |
| 17 | package com.android.server.vpn; |
| 18 | |
| 19 | import android.app.Service; |
| 20 | import android.content.Intent; |
| 21 | import android.net.vpn.IVpnService; |
| 22 | import android.net.vpn.L2tpIpsecProfile; |
Hung-ying Tyan | 46841db | 2009-07-06 23:31:33 +0800 | [diff] [blame] | 23 | import android.net.vpn.L2tpIpsecPskProfile; |
Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 24 | import android.net.vpn.L2tpProfile; |
Hung-ying Tyan | 46841db | 2009-07-06 23:31:33 +0800 | [diff] [blame] | 25 | import android.net.vpn.PptpProfile; |
Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 26 | import android.net.vpn.VpnManager; |
| 27 | import android.net.vpn.VpnProfile; |
| 28 | import android.net.vpn.VpnState; |
| 29 | import android.os.IBinder; |
Hung-ying Tyan | fe8e48cd | 2009-07-30 14:02:48 +0800 | [diff] [blame^] | 30 | import android.util.Log; |
Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 31 | |
Hung-ying Tyan | fe8e48cd | 2009-07-30 14:02:48 +0800 | [diff] [blame^] | 32 | import java.io.File; |
| 33 | import java.io.FileInputStream; |
| 34 | import java.io.FileNotFoundException; |
| 35 | import java.io.FileOutputStream; |
Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 36 | import java.io.IOException; |
Hung-ying Tyan | fe8e48cd | 2009-07-30 14:02:48 +0800 | [diff] [blame^] | 37 | import java.io.ObjectInputStream; |
| 38 | import java.io.ObjectOutputStream; |
Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * The service class for managing a VPN connection. It implements the |
| 42 | * {@link IVpnService} binder interface. |
| 43 | */ |
| 44 | public class VpnServiceBinder extends Service { |
Hung-ying Tyan | fe8e48cd | 2009-07-30 14:02:48 +0800 | [diff] [blame^] | 45 | private static final String TAG = VpnServiceBinder.class.getSimpleName(); |
| 46 | private static final boolean DBG = true; |
| 47 | |
| 48 | private static final String STATES_FILE_PATH = "/data/misc/vpn/.states"; |
Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 49 | |
| 50 | // The actual implementation is delegated to the VpnService class. |
| 51 | private VpnService<? extends VpnProfile> mService; |
| 52 | |
| 53 | private final IBinder mBinder = new IVpnService.Stub() { |
| 54 | public boolean connect(VpnProfile p, String username, String password) { |
| 55 | return VpnServiceBinder.this.connect(p, username, password); |
| 56 | } |
| 57 | |
| 58 | public void disconnect() { |
Hung-ying Tyan | 21bd4af | 2009-07-23 07:37:27 +0800 | [diff] [blame] | 59 | VpnServiceBinder.this.disconnect(); |
Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | public void checkStatus(VpnProfile p) { |
| 63 | VpnServiceBinder.this.checkStatus(p); |
| 64 | } |
| 65 | }; |
| 66 | |
Hung-ying Tyan | 21bd4af | 2009-07-23 07:37:27 +0800 | [diff] [blame] | 67 | @Override |
Hung-ying Tyan | fe8e48cd | 2009-07-30 14:02:48 +0800 | [diff] [blame^] | 68 | public void onCreate() { |
| 69 | super.onCreate(); |
| 70 | checkSavedStates(); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | @Override |
Hung-ying Tyan | 21bd4af | 2009-07-23 07:37:27 +0800 | [diff] [blame] | 75 | public void onStart(Intent intent, int startId) { |
Hung-ying Tyan | df1aa33 | 2009-07-11 22:23:30 +0800 | [diff] [blame] | 76 | super.onStart(intent, startId); |
Hung-ying Tyan | df1aa33 | 2009-07-11 22:23:30 +0800 | [diff] [blame] | 77 | } |
| 78 | |
Hung-ying Tyan | 21bd4af | 2009-07-23 07:37:27 +0800 | [diff] [blame] | 79 | @Override |
Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 80 | public IBinder onBind(Intent intent) { |
| 81 | return mBinder; |
| 82 | } |
| 83 | |
Hung-ying Tyan | fe8e48cd | 2009-07-30 14:02:48 +0800 | [diff] [blame^] | 84 | void saveStates() throws IOException { |
| 85 | if (DBG) Log.d("VpnServiceBinder", " saving states"); |
| 86 | ObjectOutputStream oos = |
| 87 | new ObjectOutputStream(new FileOutputStream(STATES_FILE_PATH)); |
| 88 | oos.writeObject(mService); |
| 89 | oos.close(); |
| 90 | } |
| 91 | |
| 92 | void removeStates() { |
| 93 | try { |
| 94 | new File(STATES_FILE_PATH).delete(); |
| 95 | } catch (Throwable e) { |
| 96 | if (DBG) Log.d("VpnServiceBinder", " remove states: " + e); |
| 97 | } |
| 98 | } |
| 99 | |
Hung-ying Tyan | 21bd4af | 2009-07-23 07:37:27 +0800 | [diff] [blame] | 100 | private synchronized boolean connect(final VpnProfile p, |
| 101 | final String username, final String password) { |
Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 102 | if (mService != null) return false; |
Hung-ying Tyan | fe8e48cd | 2009-07-30 14:02:48 +0800 | [diff] [blame^] | 103 | final VpnService s = mService = createService(p); |
Hung-ying Tyan | 21bd4af | 2009-07-23 07:37:27 +0800 | [diff] [blame] | 104 | |
| 105 | new Thread(new Runnable() { |
| 106 | public void run() { |
Hung-ying Tyan | fe8e48cd | 2009-07-30 14:02:48 +0800 | [diff] [blame^] | 107 | s.onConnect(username, password); |
Hung-ying Tyan | 21bd4af | 2009-07-23 07:37:27 +0800 | [diff] [blame] | 108 | } |
| 109 | }).start(); |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | private synchronized void disconnect() { |
| 114 | if (mService == null) return; |
Hung-ying Tyan | fe8e48cd | 2009-07-30 14:02:48 +0800 | [diff] [blame^] | 115 | final VpnService s = mService; |
Hung-ying Tyan | 21bd4af | 2009-07-23 07:37:27 +0800 | [diff] [blame] | 116 | |
| 117 | new Thread(new Runnable() { |
| 118 | public void run() { |
Hung-ying Tyan | fe8e48cd | 2009-07-30 14:02:48 +0800 | [diff] [blame^] | 119 | s.onDisconnect(); |
Hung-ying Tyan | 21bd4af | 2009-07-23 07:37:27 +0800 | [diff] [blame] | 120 | } |
| 121 | }).start(); |
Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | private synchronized void checkStatus(VpnProfile p) { |
Hung-ying Tyan | 000c3ff | 2009-07-18 10:20:10 +0800 | [diff] [blame] | 125 | if ((mService == null) |
| 126 | || (!p.getName().equals(mService.mProfile.getName()))) { |
Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 127 | broadcastConnectivity(p.getName(), VpnState.IDLE); |
| 128 | } else { |
| 129 | broadcastConnectivity(p.getName(), mService.getState()); |
| 130 | } |
| 131 | } |
| 132 | |
Hung-ying Tyan | fe8e48cd | 2009-07-30 14:02:48 +0800 | [diff] [blame^] | 133 | private void checkSavedStates() { |
| 134 | try { |
| 135 | ObjectInputStream ois = new ObjectInputStream(new FileInputStream( |
| 136 | STATES_FILE_PATH)); |
| 137 | mService = (VpnService<? extends VpnProfile>) ois.readObject(); |
| 138 | mService.recover(this); |
| 139 | ois.close(); |
| 140 | } catch (FileNotFoundException e) { |
| 141 | // do nothing |
| 142 | } catch (Throwable e) { |
| 143 | Log.i("VpnServiceBinder", "recovery error, remove states: " + e); |
| 144 | removeStates(); |
| 145 | } |
| 146 | } |
| 147 | |
Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 148 | private VpnService<? extends VpnProfile> createService(VpnProfile p) { |
| 149 | switch (p.getType()) { |
Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 150 | case L2TP: |
| 151 | L2tpService l2tp = new L2tpService(); |
| 152 | l2tp.setContext(this, (L2tpProfile) p); |
| 153 | return l2tp; |
| 154 | |
Hung-ying Tyan | 46841db | 2009-07-06 23:31:33 +0800 | [diff] [blame] | 155 | case PPTP: |
| 156 | PptpService pptp = new PptpService(); |
| 157 | pptp.setContext(this, (PptpProfile) p); |
| 158 | return pptp; |
| 159 | |
| 160 | case L2TP_IPSEC_PSK: |
| 161 | L2tpIpsecPskService psk = new L2tpIpsecPskService(); |
| 162 | psk.setContext(this, (L2tpIpsecPskProfile) p); |
| 163 | return psk; |
| 164 | |
| 165 | case L2TP_IPSEC: |
| 166 | L2tpIpsecService l2tpIpsec = new L2tpIpsecService(); |
| 167 | l2tpIpsec.setContext(this, (L2tpIpsecProfile) p); |
| 168 | return l2tpIpsec; |
| 169 | |
Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 170 | default: |
| 171 | return null; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | private void broadcastConnectivity(String name, VpnState s) { |
| 176 | new VpnManager(this).broadcastConnectivity(name, s); |
| 177 | } |
| 178 | } |