blob: 4892a7b57b6a0bb7f2d79248dce25cfe6cf72f19 [file] [log] [blame]
Hung-ying Tyanf94b6442009-06-08 13:27:11 +08001/*
Hung-ying Tyand3aba7f2009-06-19 19:45:38 +08002 * Copyright (C) 2009, The Android Open Source Project
Hung-ying Tyanf94b6442009-06-08 13:27:11 +08003 *
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.server.vpn;
18
19import android.app.Service;
20import android.content.Intent;
21import android.net.vpn.IVpnService;
22import android.net.vpn.L2tpIpsecProfile;
Hung-ying Tyan46841db2009-07-06 23:31:33 +080023import android.net.vpn.L2tpIpsecPskProfile;
Hung-ying Tyanf94b6442009-06-08 13:27:11 +080024import android.net.vpn.L2tpProfile;
Hung-ying Tyan46841db2009-07-06 23:31:33 +080025import android.net.vpn.PptpProfile;
Hung-ying Tyanf94b6442009-06-08 13:27:11 +080026import android.net.vpn.VpnManager;
27import android.net.vpn.VpnProfile;
28import android.net.vpn.VpnState;
29import android.os.IBinder;
Hung-ying Tyanfe8e48cd2009-07-30 14:02:48 +080030import android.util.Log;
Hung-ying Tyanf94b6442009-06-08 13:27:11 +080031
Hung-ying Tyanfe8e48cd2009-07-30 14:02:48 +080032import java.io.File;
33import java.io.FileInputStream;
34import java.io.FileNotFoundException;
35import java.io.FileOutputStream;
Hung-ying Tyanf94b6442009-06-08 13:27:11 +080036import java.io.IOException;
Hung-ying Tyanfe8e48cd2009-07-30 14:02:48 +080037import java.io.ObjectInputStream;
38import java.io.ObjectOutputStream;
Hung-ying Tyanf94b6442009-06-08 13:27:11 +080039
40/**
41 * The service class for managing a VPN connection. It implements the
42 * {@link IVpnService} binder interface.
43 */
44public class VpnServiceBinder extends Service {
Hung-ying Tyanfe8e48cd2009-07-30 14:02:48 +080045 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 Tyanf94b6442009-06-08 13:27:11 +080049
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 Tyan21bd4af2009-07-23 07:37:27 +080059 VpnServiceBinder.this.disconnect();
Hung-ying Tyanf94b6442009-06-08 13:27:11 +080060 }
61
62 public void checkStatus(VpnProfile p) {
63 VpnServiceBinder.this.checkStatus(p);
64 }
65 };
66
Hung-ying Tyan21bd4af2009-07-23 07:37:27 +080067 @Override
Hung-ying Tyanfe8e48cd2009-07-30 14:02:48 +080068 public void onCreate() {
69 super.onCreate();
70 checkSavedStates();
71 }
72
73
74 @Override
Hung-ying Tyan21bd4af2009-07-23 07:37:27 +080075 public void onStart(Intent intent, int startId) {
Hung-ying Tyandf1aa332009-07-11 22:23:30 +080076 super.onStart(intent, startId);
Hung-ying Tyandf1aa332009-07-11 22:23:30 +080077 }
78
Hung-ying Tyan21bd4af2009-07-23 07:37:27 +080079 @Override
Hung-ying Tyanf94b6442009-06-08 13:27:11 +080080 public IBinder onBind(Intent intent) {
81 return mBinder;
82 }
83
Hung-ying Tyanfe8e48cd2009-07-30 14:02:48 +080084 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 Tyan21bd4af2009-07-23 07:37:27 +0800100 private synchronized boolean connect(final VpnProfile p,
101 final String username, final String password) {
Hung-ying Tyanf94b6442009-06-08 13:27:11 +0800102 if (mService != null) return false;
Hung-ying Tyanfe8e48cd2009-07-30 14:02:48 +0800103 final VpnService s = mService = createService(p);
Hung-ying Tyan21bd4af2009-07-23 07:37:27 +0800104
105 new Thread(new Runnable() {
106 public void run() {
Hung-ying Tyanfe8e48cd2009-07-30 14:02:48 +0800107 s.onConnect(username, password);
Hung-ying Tyan21bd4af2009-07-23 07:37:27 +0800108 }
109 }).start();
110 return true;
111 }
112
113 private synchronized void disconnect() {
114 if (mService == null) return;
Hung-ying Tyanfe8e48cd2009-07-30 14:02:48 +0800115 final VpnService s = mService;
Hung-ying Tyan21bd4af2009-07-23 07:37:27 +0800116
117 new Thread(new Runnable() {
118 public void run() {
Hung-ying Tyanfe8e48cd2009-07-30 14:02:48 +0800119 s.onDisconnect();
Hung-ying Tyan21bd4af2009-07-23 07:37:27 +0800120 }
121 }).start();
Hung-ying Tyanf94b6442009-06-08 13:27:11 +0800122 }
123
124 private synchronized void checkStatus(VpnProfile p) {
Hung-ying Tyan000c3ff2009-07-18 10:20:10 +0800125 if ((mService == null)
126 || (!p.getName().equals(mService.mProfile.getName()))) {
Hung-ying Tyanf94b6442009-06-08 13:27:11 +0800127 broadcastConnectivity(p.getName(), VpnState.IDLE);
128 } else {
129 broadcastConnectivity(p.getName(), mService.getState());
130 }
131 }
132
Hung-ying Tyanfe8e48cd2009-07-30 14:02:48 +0800133 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 Tyanf94b6442009-06-08 13:27:11 +0800148 private VpnService<? extends VpnProfile> createService(VpnProfile p) {
149 switch (p.getType()) {
Hung-ying Tyanf94b6442009-06-08 13:27:11 +0800150 case L2TP:
151 L2tpService l2tp = new L2tpService();
152 l2tp.setContext(this, (L2tpProfile) p);
153 return l2tp;
154
Hung-ying Tyan46841db2009-07-06 23:31:33 +0800155 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 Tyanf94b6442009-06-08 13:27:11 +0800170 default:
171 return null;
172 }
173 }
174
175 private void broadcastConnectivity(String name, VpnState s) {
176 new VpnManager(this).broadcastConnectivity(name, s);
177 }
178}