Hung-ying Tyan | d3aba7f | 2009-06-19 19:45:38 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009, 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 | |
| 17 | package com.android.server.vpn; |
| 18 | |
| 19 | import java.io.IOException; |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.Arrays; |
| 22 | |
| 23 | /** |
| 24 | * A helper class for sending commands to the MTP daemon (mtpd). |
| 25 | */ |
| 26 | class MtpdHelper { |
Hung-ying Tyan | fe8e48cd | 2009-07-30 14:02:48 +0800 | [diff] [blame^] | 27 | static final String MTPD = "mtpd"; |
Hung-ying Tyan | d3aba7f | 2009-06-19 19:45:38 +0800 | [diff] [blame] | 28 | private static final String VPN_LINKNAME = "vpn"; |
| 29 | private static final String PPP_ARGS_SEPARATOR = ""; |
| 30 | |
| 31 | static void sendCommand(VpnService<?> vpnService, String protocol, |
| 32 | String serverIp, String port, String secret, String username, |
| 33 | String password) throws IOException { |
Hung-ying Tyan | fe8e48cd | 2009-07-30 14:02:48 +0800 | [diff] [blame^] | 34 | sendCommand(vpnService, protocol, serverIp, port, secret, username, |
| 35 | password, false); |
| 36 | } |
| 37 | |
| 38 | static void sendCommand(VpnService<?> vpnService, String protocol, |
| 39 | String serverIp, String port, String secret, String username, |
| 40 | String password, boolean encryption) throws IOException { |
Hung-ying Tyan | d3aba7f | 2009-06-19 19:45:38 +0800 | [diff] [blame] | 41 | ArrayList<String> args = new ArrayList<String>(); |
| 42 | args.addAll(Arrays.asList(protocol, serverIp, port)); |
| 43 | if (secret != null) args.add(secret); |
| 44 | args.add(PPP_ARGS_SEPARATOR); |
Hung-ying Tyan | fe8e48cd | 2009-07-30 14:02:48 +0800 | [diff] [blame^] | 45 | addPppArguments(args, serverIp, username, password, encryption); |
Hung-ying Tyan | d3aba7f | 2009-06-19 19:45:38 +0800 | [diff] [blame] | 46 | |
Hung-ying Tyan | 21bd4af | 2009-07-23 07:37:27 +0800 | [diff] [blame] | 47 | DaemonProxy mtpd = vpnService.startDaemon(MTPD); |
Hung-ying Tyan | 2b04d29 | 2009-06-27 15:59:44 +0800 | [diff] [blame] | 48 | mtpd.sendCommand(args.toArray(new String[args.size()])); |
Hung-ying Tyan | d3aba7f | 2009-06-19 19:45:38 +0800 | [diff] [blame] | 49 | } |
| 50 | |
Hung-ying Tyan | fe8e48cd | 2009-07-30 14:02:48 +0800 | [diff] [blame^] | 51 | private static void addPppArguments(ArrayList<String> args, String serverIp, |
| 52 | String username, String password, boolean encryption) |
| 53 | throws IOException { |
Hung-ying Tyan | d3aba7f | 2009-06-19 19:45:38 +0800 | [diff] [blame] | 54 | args.addAll(Arrays.asList( |
| 55 | "linkname", VPN_LINKNAME, |
| 56 | "name", username, |
| 57 | "password", password, |
Hung-ying Tyan | d3aba7f | 2009-06-19 19:45:38 +0800 | [diff] [blame] | 58 | "refuse-eap", "nodefaultroute", "usepeerdns", |
| 59 | "idle", "1800", |
| 60 | "mtu", "1400", |
| 61 | "mru", "1400")); |
Hung-ying Tyan | fe8e48cd | 2009-07-30 14:02:48 +0800 | [diff] [blame^] | 62 | if (encryption) { |
| 63 | args.add("+mppe"); |
| 64 | } |
Hung-ying Tyan | d3aba7f | 2009-06-19 19:45:38 +0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | private MtpdHelper() { |
| 68 | } |
| 69 | } |