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 <stdio.h>
18#include <string.h>
19#include <stdlib.h>
20#include <errno.h>
21#include <sys/socket.h>
22#include <netinet/in.h>
23#include <arpa/inet.h>
24
25#include "PropertyManager.h"
26#include "VpnController.h"
27
28VpnController::VpnController(PropertyManager *propmngr,
29                             IControllerHandler *handlers) :
30               Controller("vpn", propmngr, handlers) {
31    mEnabled = false;
32
33    mStaticProperties.propEnabled = new VpnEnabledProperty(this);
34    mDynamicProperties.propGateway = new IPV4AddressPropertyHelper("Gateway",
35                                                                   false,
36                                                                   &mGateway);
37}
38
39int VpnController::start() {
40    mPropMngr->attachProperty("vpn", mStaticProperties.propEnabled);
41    return 0;
42}
43
44int VpnController::stop() {
45    mPropMngr->detachProperty("vpn", mStaticProperties.propEnabled);
46    return 0;
47}
48
49VpnController::VpnIntegerProperty::VpnIntegerProperty(VpnController *c,
50                                                      const char *name,
51                                                      bool ro,
52                                                      int elements) :
53                IntegerProperty(name, ro, elements) {
54    mVc = c;
55}
56
57VpnController::VpnStringProperty::VpnStringProperty(VpnController *c,
58                                                    const char *name,
59                                                    bool ro, int elements) :
60                StringProperty(name, ro, elements) {
61    mVc = c;
62}
63
64VpnController::VpnIPV4AddressProperty::VpnIPV4AddressProperty(VpnController *c,
65                                                              const char *name,
66                                                              bool ro, int elements) :
67                IPV4AddressProperty(name, ro, elements) {
68    mVc = c;
69}
70
71VpnController::VpnEnabledProperty::VpnEnabledProperty(VpnController *c) :
72                VpnIntegerProperty(c, "Enabled", false, 1) {
73}
74int VpnController::VpnEnabledProperty::get(int idx, int *buffer) {
75    *buffer = mVc->mEnabled;
76    return 0;
77}
78int VpnController::VpnEnabledProperty::set(int idx, int value) {
79    int rc;
80    if (!value) {
81        mVc->mPropMngr->detachProperty("vpn", mVc->mDynamicProperties.propGateway);
82        rc = mVc->disable();
83    } else {
84        rc = mVc->enable();
85        if (!rc) {
86            mVc->mPropMngr->attachProperty("vpn", mVc->mDynamicProperties.propGateway);
87        }
88    }
89    if (!rc)
90        mVc->mEnabled = value;
91    return rc;
92}
93