1/*
2 * Copyright (C) 2008 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#include <errno.h>
18#include <netinet/in.h>
19#include <arpa/inet.h>
20
21#define LOG_TAG "OpenVpnController"
22#include <cutils/log.h>
23#include <cutils/properties.h>
24
25#include <sysutils/ServiceManager.h>
26
27#include "OpenVpnController.h"
28#include "PropertyManager.h"
29
30#define DAEMON_PROP_NAME "vpn.openvpn.status"
31#define DAEMON_CONFIG_FILE "/data/misc/openvpn/openvpn.conf"
32
33OpenVpnController::OpenVpnController(PropertyManager *propmngr,
34                                     IControllerHandler *handlers) :
35                   VpnController(propmngr, handlers) {
36    mServiceManager = new ServiceManager();
37}
38
39OpenVpnController::~OpenVpnController() {
40    delete mServiceManager;
41}
42
43int OpenVpnController::start() {
44    return VpnController::start();
45}
46
47int OpenVpnController::stop() {
48    return VpnController::stop();
49}
50
51int OpenVpnController::enable() {
52    char svc[PROPERTY_VALUE_MAX];
53    char tmp[64];
54
55    if (!mPropMngr->get("vpn.gateway", tmp, sizeof(tmp))) {
56        LOGE("Error reading property 'vpn.gateway' (%s)", strerror(errno));
57        return -1;
58    }
59    snprintf(svc, sizeof(svc), "openvpn:--remote %s 1194", tmp);
60
61    if (mServiceManager->start(svc))
62        return -1;
63
64    return 0;
65}
66
67int OpenVpnController::disable() {
68    if (mServiceManager->stop("openvpn"))
69        return -1;
70    return 0;
71}
72