blob: 1246703abbde9796ed0e1110d095afa40eca136d [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 Mehat3c5a6f02009-05-22 15:36:13 -070028VpnController::VpnController(PropertyManager *propmngr) :
29 Controller("VPN", propmngr) {
30 mEnabled = false;
31 propmngr->registerProperty("vpn.enabled", this);
32 propmngr->registerProperty("vpn.gateway", this);
San Mehatdc266072009-05-06 11:16:52 -070033}
34
35int VpnController::start() {
San Mehat3c5a6f02009-05-22 15:36:13 -070036 return 0;
San Mehatdc266072009-05-06 11:16:52 -070037}
38
39int VpnController::stop() {
San Mehat3c5a6f02009-05-22 15:36:13 -070040 return 0;
San Mehatdc266072009-05-06 11:16:52 -070041}
42
San Mehat3c5a6f02009-05-22 15:36:13 -070043int VpnController::set(const char *name, const char *value) {
44 if (!strcmp(name, "vpn.enabled")) {
45 int en = atoi(value);
46 int rc;
San Mehatdc266072009-05-06 11:16:52 -070047
San Mehat3c5a6f02009-05-22 15:36:13 -070048 if (en == mEnabled)
49 return 0;
50 rc = (en ? enable() : disable());
San Mehat5d6d4172009-05-14 15:00:06 -070051
San Mehat3c5a6f02009-05-22 15:36:13 -070052 if (!rc)
53 mEnabled = en;
54 return rc;
55 } if (!strcmp(name, "vpn.gateway")) {
San Mehat48765672009-05-20 15:28:43 -070056 if (!inet_aton(value, &mVpnGateway)) {
57 errno = EINVAL;
58 return -1;
59 }
60 return 0;
San Mehat3c5a6f02009-05-22 15:36:13 -070061 }
San Mehat48765672009-05-20 15:28:43 -070062
San Mehat3c5a6f02009-05-22 15:36:13 -070063 return Controller::set(name, value);
San Mehat5d6d4172009-05-14 15:00:06 -070064}
65
San Mehat3c5a6f02009-05-22 15:36:13 -070066const char *VpnController::get(const char *name, char *buffer, size_t maxsize) {
67 if (!strcmp(name, "vpn.enabled")) {
68 snprintf(buffer, maxsize, "%d", mEnabled);
69 return buffer;
70 } if (!strcmp(name, "vpn.gateway")) {
San Mehat48765672009-05-20 15:28:43 -070071 snprintf(buffer, maxsize, "%s", inet_ntoa(mVpnGateway));
72 return buffer;
73 }
74
San Mehat3c5a6f02009-05-22 15:36:13 -070075 return Controller::get(name, buffer, maxsize);
San Mehat5d6d4172009-05-14 15:00:06 -070076}