San Mehat | 94447ca | 2009-05-13 11:54:16 -0700 | [diff] [blame] | 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 | #include <errno.h> |
San Mehat | 5d6d417 | 2009-05-14 15:00:06 -0700 | [diff] [blame^] | 17 | #include <netinet/in.h> |
| 18 | #include <arpa/inet.h> |
San Mehat | 94447ca | 2009-05-13 11:54:16 -0700 | [diff] [blame] | 19 | |
| 20 | #define LOG_TAG "OpenVpnController" |
| 21 | #include <cutils/log.h> |
| 22 | #include <cutils/properties.h> |
| 23 | |
San Mehat | 5d6d417 | 2009-05-14 15:00:06 -0700 | [diff] [blame^] | 24 | #include <sysutils/ServiceManager.h> |
| 25 | |
San Mehat | 94447ca | 2009-05-13 11:54:16 -0700 | [diff] [blame] | 26 | #include "OpenVpnController.h" |
| 27 | |
| 28 | #define DAEMON_PROP_NAME "vpn.openvpn.status" |
San Mehat | 5d6d417 | 2009-05-14 15:00:06 -0700 | [diff] [blame^] | 29 | #define DAEMON_CONFIG_FILE "/data/misc/openvpn/openvpn.conf" |
San Mehat | 94447ca | 2009-05-13 11:54:16 -0700 | [diff] [blame] | 30 | |
| 31 | OpenVpnController::OpenVpnController() : |
| 32 | VpnController() { |
San Mehat | 5d6d417 | 2009-05-14 15:00:06 -0700 | [diff] [blame^] | 33 | mServiceManager = new ServiceManager(); |
| 34 | } |
| 35 | |
| 36 | OpenVpnController::~OpenVpnController() { |
| 37 | delete mServiceManager; |
San Mehat | 94447ca | 2009-05-13 11:54:16 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | int OpenVpnController::start() { |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | int OpenVpnController::stop() { |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | int OpenVpnController::enable() { |
| 49 | |
San Mehat | 5d6d417 | 2009-05-14 15:00:06 -0700 | [diff] [blame^] | 50 | if (validateConfig()) { |
| 51 | LOGE("Error validating configuration file"); |
San Mehat | 94447ca | 2009-05-13 11:54:16 -0700 | [diff] [blame] | 52 | return -1; |
| 53 | } |
| 54 | |
San Mehat | 5d6d417 | 2009-05-14 15:00:06 -0700 | [diff] [blame^] | 55 | if (mServiceManager->start("openvpn")) |
| 56 | return -1; |
| 57 | |
San Mehat | 94447ca | 2009-05-13 11:54:16 -0700 | [diff] [blame] | 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | int OpenVpnController::disable() { |
San Mehat | 5d6d417 | 2009-05-14 15:00:06 -0700 | [diff] [blame^] | 62 | |
| 63 | if (mServiceManager->stop("openvpn")) |
| 64 | return -1; |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | int OpenVpnController::validateConfig() { |
| 69 | unlink(DAEMON_CONFIG_FILE); |
| 70 | |
| 71 | FILE *fp = fopen(DAEMON_CONFIG_FILE, "w"); |
| 72 | if (!fp) |
| 73 | return -1; |
| 74 | |
| 75 | fprintf(fp, "remote %s 1194\n", inet_ntoa(getVpnGateway())); |
| 76 | fclose(fp); |
| 77 | return 0; |
San Mehat | 94447ca | 2009-05-13 11:54:16 -0700 | [diff] [blame] | 78 | } |