San Mehat | dc26607 | 2009-05-06 11:16:52 -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 | */ |
San Mehat | 5d6d417 | 2009-05-14 15:00:06 -0700 | [diff] [blame] | 16 | |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 17 | #include <stdio.h> |
San Mehat | 5d6d417 | 2009-05-14 15:00:06 -0700 | [diff] [blame] | 18 | #include <string.h> |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 20 | #include <errno.h> |
San Mehat | 5d6d417 | 2009-05-14 15:00:06 -0700 | [diff] [blame] | 21 | #include <sys/socket.h> |
| 22 | #include <netinet/in.h> |
| 23 | #include <arpa/inet.h> |
| 24 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 25 | #include "PropertyManager.h" |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 26 | #include "VpnController.h" |
| 27 | |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame] | 28 | VpnController::VpnController(PropertyManager *propmngr, |
| 29 | IControllerHandler *handlers) : |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 30 | Controller("vpn", propmngr, handlers) { |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 31 | mEnabled = false; |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 32 | |
| 33 | mStaticProperties.propEnabled = new VpnEnabledProperty(this); |
| 34 | mDynamicProperties.propGateway = new IPV4AddressPropertyHelper("Gateway", |
| 35 | false, |
| 36 | &mGateway); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | int VpnController::start() { |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 40 | mPropMngr->attachProperty("vpn", mStaticProperties.propEnabled); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 41 | return 0; |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | int VpnController::stop() { |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 45 | mPropMngr->detachProperty("vpn", mStaticProperties.propEnabled); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 46 | return 0; |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 47 | } |
| 48 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 49 | VpnController::VpnIntegerProperty::VpnIntegerProperty(VpnController *c, |
| 50 | const char *name, |
| 51 | bool ro, |
| 52 | int elements) : |
| 53 | IntegerProperty(name, ro, elements) { |
| 54 | mVc = c; |
San Mehat | 5d6d417 | 2009-05-14 15:00:06 -0700 | [diff] [blame] | 55 | } |
| 56 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 57 | VpnController::VpnStringProperty::VpnStringProperty(VpnController *c, |
| 58 | const char *name, |
| 59 | bool ro, int elements) : |
| 60 | StringProperty(name, ro, elements) { |
| 61 | mVc = c; |
| 62 | } |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 63 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 64 | VpnController::VpnIPV4AddressProperty::VpnIPV4AddressProperty(VpnController *c, |
| 65 | const char *name, |
| 66 | bool ro, int elements) : |
| 67 | IPV4AddressProperty(name, ro, elements) { |
| 68 | mVc = c; |
| 69 | } |
| 70 | |
| 71 | VpnController::VpnEnabledProperty::VpnEnabledProperty(VpnController *c) : |
| 72 | VpnIntegerProperty(c, "Enabled", false, 1) { |
| 73 | } |
| 74 | int VpnController::VpnEnabledProperty::get(int idx, int *buffer) { |
| 75 | *buffer = mVc->mEnabled; |
| 76 | return 0; |
| 77 | } |
| 78 | int 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 Mehat | 5d6d417 | 2009-05-14 15:00:06 -0700 | [diff] [blame] | 92 | } |