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 | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 28 | VpnController::VpnController(PropertyManager *propmngr) : |
| 29 | Controller("VPN", propmngr) { |
| 30 | mEnabled = false; |
| 31 | propmngr->registerProperty("vpn.enabled", this); |
| 32 | propmngr->registerProperty("vpn.gateway", this); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | int VpnController::start() { |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 36 | return 0; |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | int VpnController::stop() { |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 40 | return 0; |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 41 | } |
| 42 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 43 | int VpnController::set(const char *name, const char *value) { |
| 44 | if (!strcmp(name, "vpn.enabled")) { |
| 45 | int en = atoi(value); |
| 46 | int rc; |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 47 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 48 | if (en == mEnabled) |
| 49 | return 0; |
| 50 | rc = (en ? enable() : disable()); |
San Mehat | 5d6d417 | 2009-05-14 15:00:06 -0700 | [diff] [blame] | 51 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 52 | if (!rc) |
| 53 | mEnabled = en; |
| 54 | return rc; |
| 55 | } if (!strcmp(name, "vpn.gateway")) { |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 56 | if (!inet_aton(value, &mVpnGateway)) { |
| 57 | errno = EINVAL; |
| 58 | return -1; |
| 59 | } |
| 60 | return 0; |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 61 | } |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 62 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 63 | return Controller::set(name, value); |
San Mehat | 5d6d417 | 2009-05-14 15:00:06 -0700 | [diff] [blame] | 64 | } |
| 65 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 66 | const 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 Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 71 | snprintf(buffer, maxsize, "%s", inet_ntoa(mVpnGateway)); |
| 72 | return buffer; |
| 73 | } |
| 74 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 75 | return Controller::get(name, buffer, maxsize); |
San Mehat | 5d6d417 | 2009-05-14 15:00:06 -0700 | [diff] [blame] | 76 | } |