blob: 015710fcc53bf03f5cb1f9b4cca44e087df2fa31 [file] [log] [blame]
San Mehatdc266072009-05-06 11:16:52 -07001/*
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 */
San Mehat5d6d4172009-05-14 15:00:06 -070016
San Mehat48765672009-05-20 15:28:43 -070017#include <stdio.h>
San Mehat5d6d4172009-05-14 15:00:06 -070018#include <string.h>
San Mehat3c5a6f02009-05-22 15:36:13 -070019#include <stdlib.h>
San Mehatdc266072009-05-06 11:16:52 -070020#include <errno.h>
San Mehat5d6d4172009-05-14 15:00:06 -070021#include <sys/socket.h>
22#include <netinet/in.h>
23#include <arpa/inet.h>
24
San Mehat3c5a6f02009-05-22 15:36:13 -070025#include "PropertyManager.h"
San Mehatdc266072009-05-06 11:16:52 -070026#include "VpnController.h"
27
San Mehat3aff2d12009-06-15 14:10:44 -070028VpnController::VpnController(PropertyManager *propmngr,
29 IControllerHandler *handlers) :
San Mehatc4a895b2009-06-23 21:10:57 -070030 Controller("vpn", propmngr, handlers) {
San Mehat3c5a6f02009-05-22 15:36:13 -070031 mEnabled = false;
San Mehatc4a895b2009-06-23 21:10:57 -070032
33 mStaticProperties.propEnabled = new VpnEnabledProperty(this);
34 mDynamicProperties.propGateway = new IPV4AddressPropertyHelper("Gateway",
35 false,
36 &mGateway);
San Mehatdc266072009-05-06 11:16:52 -070037}
38
39int VpnController::start() {
San Mehatc4a895b2009-06-23 21:10:57 -070040 mPropMngr->attachProperty("vpn", mStaticProperties.propEnabled);
San Mehat3c5a6f02009-05-22 15:36:13 -070041 return 0;
San Mehatdc266072009-05-06 11:16:52 -070042}
43
44int VpnController::stop() {
San Mehatc4a895b2009-06-23 21:10:57 -070045 mPropMngr->detachProperty("vpn", mStaticProperties.propEnabled);
San Mehat3c5a6f02009-05-22 15:36:13 -070046 return 0;
San Mehatdc266072009-05-06 11:16:52 -070047}
48
San Mehatc4a895b2009-06-23 21:10:57 -070049VpnController::VpnIntegerProperty::VpnIntegerProperty(VpnController *c,
50 const char *name,
51 bool ro,
52 int elements) :
53 IntegerProperty(name, ro, elements) {
54 mVc = c;
San Mehat5d6d4172009-05-14 15:00:06 -070055}
56
San Mehatc4a895b2009-06-23 21:10:57 -070057VpnController::VpnStringProperty::VpnStringProperty(VpnController *c,
58 const char *name,
59 bool ro, int elements) :
60 StringProperty(name, ro, elements) {
61 mVc = c;
62}
San Mehat48765672009-05-20 15:28:43 -070063
San Mehatc4a895b2009-06-23 21:10:57 -070064VpnController::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;
San Mehat5d6d4172009-05-14 15:00:06 -070092}