blob: 9078d9bbe4e03bdd39f36617e6e55207f03aa9db [file] [log] [blame]
Hung-ying Tyand3aba7f2009-06-19 19:45:38 +08001/*
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
17package com.android.server.vpn;
18
19import java.io.IOException;
20import java.util.ArrayList;
21import java.util.Arrays;
22
23/**
24 * A helper class for sending commands to the MTP daemon (mtpd).
25 */
26class MtpdHelper {
Hung-ying Tyanfe8e48cd2009-07-30 14:02:48 +080027 static final String MTPD = "mtpd";
Hung-ying Tyand3aba7f2009-06-19 19:45:38 +080028 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 Tyanfe8e48cd2009-07-30 14:02:48 +080034 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 Tyand3aba7f2009-06-19 19:45:38 +080041 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 Tyanfe8e48cd2009-07-30 14:02:48 +080045 addPppArguments(args, serverIp, username, password, encryption);
Hung-ying Tyand3aba7f2009-06-19 19:45:38 +080046
Hung-ying Tyan21bd4af2009-07-23 07:37:27 +080047 DaemonProxy mtpd = vpnService.startDaemon(MTPD);
Hung-ying Tyan2b04d292009-06-27 15:59:44 +080048 mtpd.sendCommand(args.toArray(new String[args.size()]));
Hung-ying Tyand3aba7f2009-06-19 19:45:38 +080049 }
50
Hung-ying Tyanfe8e48cd2009-07-30 14:02:48 +080051 private static void addPppArguments(ArrayList<String> args, String serverIp,
52 String username, String password, boolean encryption)
53 throws IOException {
Hung-ying Tyand3aba7f2009-06-19 19:45:38 +080054 args.addAll(Arrays.asList(
55 "linkname", VPN_LINKNAME,
56 "name", username,
57 "password", password,
Hung-ying Tyand3aba7f2009-06-19 19:45:38 +080058 "refuse-eap", "nodefaultroute", "usepeerdns",
59 "idle", "1800",
60 "mtu", "1400",
61 "mru", "1400"));
Hung-ying Tyanfe8e48cd2009-07-30 14:02:48 +080062 if (encryption) {
63 args.add("+mppe");
64 }
Hung-ying Tyand3aba7f2009-06-19 19:45:38 +080065 }
66
67 private MtpdHelper() {
68 }
69}