blob: add4dc39a0a625e8dfd02a623bda2c5731dbc0a1 [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) :
30 Controller("VPN", propmngr, handlers) {
San Mehat3c5a6f02009-05-22 15:36:13 -070031 mEnabled = false;
San Mehatdc266072009-05-06 11:16:52 -070032}
33
34int VpnController::start() {
San Mehat3aff2d12009-06-15 14:10:44 -070035 mPropMngr->registerProperty("vpn.enabled", this);
San Mehat3c5a6f02009-05-22 15:36:13 -070036 return 0;
San Mehatdc266072009-05-06 11:16:52 -070037}
38
39int VpnController::stop() {
San Mehat3aff2d12009-06-15 14:10:44 -070040 mPropMngr->unregisterProperty("vpn.enabled");
San Mehat3c5a6f02009-05-22 15:36:13 -070041 return 0;
San Mehatdc266072009-05-06 11:16:52 -070042}
43
San Mehat3c5a6f02009-05-22 15:36:13 -070044int VpnController::set(const char *name, const char *value) {
45 if (!strcmp(name, "vpn.enabled")) {
46 int en = atoi(value);
47 int rc;
San Mehatdc266072009-05-06 11:16:52 -070048
San Mehat3c5a6f02009-05-22 15:36:13 -070049 if (en == mEnabled)
50 return 0;
51 rc = (en ? enable() : disable());
San Mehat5d6d4172009-05-14 15:00:06 -070052
San Mehat3aff2d12009-06-15 14:10:44 -070053 if (!rc) {
San Mehat3c5a6f02009-05-22 15:36:13 -070054 mEnabled = en;
San Mehat3aff2d12009-06-15 14:10:44 -070055 if (en)
56 mPropMngr->unregisterProperty("vpn.gateway");
57 else
58 mPropMngr->unregisterProperty("vpn.gateway");
59 }
San Mehat3c5a6f02009-05-22 15:36:13 -070060 return rc;
61 } if (!strcmp(name, "vpn.gateway")) {
San Mehat48765672009-05-20 15:28:43 -070062 if (!inet_aton(value, &mVpnGateway)) {
63 errno = EINVAL;
64 return -1;
65 }
66 return 0;
San Mehat3c5a6f02009-05-22 15:36:13 -070067 }
San Mehat48765672009-05-20 15:28:43 -070068
San Mehat3c5a6f02009-05-22 15:36:13 -070069 return Controller::set(name, value);
San Mehat5d6d4172009-05-14 15:00:06 -070070}
71
San Mehat3c5a6f02009-05-22 15:36:13 -070072const char *VpnController::get(const char *name, char *buffer, size_t maxsize) {
73 if (!strcmp(name, "vpn.enabled")) {
74 snprintf(buffer, maxsize, "%d", mEnabled);
75 return buffer;
76 } if (!strcmp(name, "vpn.gateway")) {
San Mehat48765672009-05-20 15:28:43 -070077 snprintf(buffer, maxsize, "%s", inet_ntoa(mVpnGateway));
78 return buffer;
79 }
80
San Mehat3c5a6f02009-05-22 15:36:13 -070081 return Controller::get(name, buffer, maxsize);
San Mehat5d6d4172009-05-14 15:00:06 -070082}