Loading nexus/CommandListener.cpp +79 −0 Original line number Diff line number Diff line Loading @@ -14,6 +14,9 @@ * limitations under the License. */ #include <stdlib.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <errno.h> #define LOG_TAG "CommandListener" Loading @@ -25,6 +28,7 @@ #include "Controller.h" #include "NetworkManager.h" #include "WifiController.h" #include "VpnController.h" #include "ErrorCode.h" CommandListener::CommandListener() : Loading @@ -40,6 +44,8 @@ CommandListener::CommandListener() : registerCmd(new WifiGetVarCmd()); registerCmd(new VpnEnableCmd()); registerCmd(new VpnSetVarCmd()); registerCmd(new VpnGetVarCmd()); registerCmd(new VpnDisableCmd()); } Loading Loading @@ -261,6 +267,79 @@ int CommandListener::VpnEnableCmd::runCommand(SocketClient *cli, char *data) { return 0; } CommandListener::VpnSetVarCmd::VpnSetVarCmd() : NexusCommand("vpn_setvar") { } int CommandListener::VpnSetVarCmd::runCommand(SocketClient *cli, char *data) { VpnController *vc = (VpnController *) NetworkManager::Instance()->findController("VPN"); char *bword; char *last; char varname[32]; char val[250]; if (!(bword = strtok_r(data, ":", &last))) goto out_inval; strncpy(varname, bword, sizeof(varname)); if (!(bword = strtok_r(NULL, ":", &last))) goto out_inval; strncpy(val, bword, sizeof(val)); if (!strcasecmp(varname, "vpn_gateway")) { if (vc->setVpnGateway(val)) goto out_inval; } else { cli->sendMsg(ErrorCode::CommandParameterError, "Variable not found.", true); return 0; } cli->sendMsg(ErrorCode::CommandOkay, "Variable written.", false); return 0; out_inval: errno = EINVAL; cli->sendMsg(ErrorCode::CommandParameterError, "Failed to set variable.", true); return 0; } CommandListener::VpnGetVarCmd::VpnGetVarCmd() : NexusCommand("vpn_getvar") { } int CommandListener::VpnGetVarCmd::runCommand(SocketClient *cli, char *data) { VpnController *vc = (VpnController *) NetworkManager::Instance()->findController("VPN"); char *bword; char *last; char varname[32]; if (!(bword = strtok_r(data, ":", &last))) goto out_inval; strncpy(varname, bword, sizeof(varname)); if (!strcasecmp(varname, "vpn_gateway")) { char buffer[255]; sprintf(buffer, "%s:%s", varname, inet_ntoa(vc->getVpnGateway())); cli->sendMsg(ErrorCode::VariableRead, buffer, false); } else { cli->sendMsg(ErrorCode::CommandParameterError, "Variable not found.", true); return 0; } cli->sendMsg(ErrorCode::CommandOkay, "Variable read.", false); return 0; out_inval: errno = EINVAL; cli->sendMsg(ErrorCode::CommandParameterError, "Failed to get variable.", true); return 0; } CommandListener::VpnDisableCmd::VpnDisableCmd() : NexusCommand("vpn_disable") { } Loading nexus/CommandListener.h +14 −0 Original line number Diff line number Diff line Loading @@ -95,6 +95,20 @@ private: int runCommand(SocketClient *c, char *data); }; class VpnSetVarCmd : public NexusCommand { public: VpnSetVarCmd(); virtual ~VpnSetVarCmd() {} int runCommand(SocketClient *c, char *data); }; class VpnGetVarCmd : public NexusCommand { public: VpnGetVarCmd(); virtual ~VpnGetVarCmd() {} int runCommand(SocketClient *c, char *data); }; class VpnDisableCmd : public NexusCommand { public: VpnDisableCmd(); Loading nexus/ErrorCode.h +3 −0 Original line number Diff line number Diff line Loading @@ -26,6 +26,9 @@ public: static const int WifiScanResult = 125; static const int WifiNetworkList = 126; static const int VariableRead = 127; static const int VariableWrite = 128; // 200 series - Requested action has been successfully completed static const int CommandOkay = 200; Loading nexus/OpenVpnController.cpp +27 −53 Original line number Diff line number Diff line Loading @@ -14,17 +14,27 @@ * limitations under the License. */ #include <errno.h> #include <netinet/in.h> #include <arpa/inet.h> #define LOG_TAG "OpenVpnController" #include <cutils/log.h> #include <cutils/properties.h> #include <sysutils/ServiceManager.h> #include "OpenVpnController.h" #define DAEMON_PROP_NAME "vpn.openvpn.status" #define DAEMON_CONFIG_FILE "/data/misc/openvpn/openvpn.conf" OpenVpnController::OpenVpnController() : VpnController() { mServiceManager = new ServiceManager(); } OpenVpnController::~OpenVpnController() { delete mServiceManager; } int OpenVpnController::start() { Loading @@ -37,68 +47,32 @@ int OpenVpnController::stop() { int OpenVpnController::enable() { // Validate configuration file // Validate key file if (startServiceDaemon()) return -1; errno = -ENOSYS; if (validateConfig()) { LOGE("Error validating configuration file"); return -1; } int OpenVpnController::startServiceDaemon() { char status[PROPERTY_VALUE_MAX]; int count = 100; property_set("ctl.start", "openvpn"); sched_yield(); if (mServiceManager->start("openvpn")) return -1; while (count-- > 0) { if (property_get(DAEMON_PROP_NAME, status, NULL)) { if (strcmp(status, "ok") == 0) return 0; else if (strcmp(DAEMON_PROP_NAME, "failed") == 0) return -1; } usleep(200000); } property_set(DAEMON_PROP_NAME, "timeout"); return -1; } int OpenVpnController::stopServiceDaemon() { char status[PROPERTY_VALUE_MAX] = {'\0'}; int count = 50; int OpenVpnController::disable() { if (property_get(DAEMON_PROP_NAME, status, NULL) && !strcmp(status, "stopped")) { LOGD("Service already stopped"); if (mServiceManager->stop("openvpn")) return -1; return 0; } property_set("ctl.stop", "openvpn"); sched_yield(); while (count-- > 0) { if (property_get(DAEMON_PROP_NAME, status, NULL)) { if (!strcmp(status, "stopped")) break; } usleep(100000); } int OpenVpnController::validateConfig() { unlink(DAEMON_CONFIG_FILE); if (!count) { LOGD("Timed out waiting for openvpn to stop"); errno = ETIMEDOUT; FILE *fp = fopen(DAEMON_CONFIG_FILE, "w"); if (!fp) return -1; } fprintf(fp, "remote %s 1194\n", inet_ntoa(getVpnGateway())); fclose(fp); return 0; } int OpenVpnController::disable() { errno = -ENOSYS; return -1; } nexus/OpenVpnController.h +6 −3 Original line number Diff line number Diff line Loading @@ -19,11 +19,15 @@ #include "VpnController.h" class ServiceManager; class OpenVpnController : public VpnController { private: ServiceManager *mServiceManager; public: OpenVpnController(); virtual ~OpenVpnController() {} virtual ~OpenVpnController(); int start(); int stop(); Loading @@ -33,8 +37,7 @@ public: protected: private: int startServiceDaemon(); int stopServiceDaemon(); int validateConfig(); }; #endif Loading
nexus/CommandListener.cpp +79 −0 Original line number Diff line number Diff line Loading @@ -14,6 +14,9 @@ * limitations under the License. */ #include <stdlib.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <errno.h> #define LOG_TAG "CommandListener" Loading @@ -25,6 +28,7 @@ #include "Controller.h" #include "NetworkManager.h" #include "WifiController.h" #include "VpnController.h" #include "ErrorCode.h" CommandListener::CommandListener() : Loading @@ -40,6 +44,8 @@ CommandListener::CommandListener() : registerCmd(new WifiGetVarCmd()); registerCmd(new VpnEnableCmd()); registerCmd(new VpnSetVarCmd()); registerCmd(new VpnGetVarCmd()); registerCmd(new VpnDisableCmd()); } Loading Loading @@ -261,6 +267,79 @@ int CommandListener::VpnEnableCmd::runCommand(SocketClient *cli, char *data) { return 0; } CommandListener::VpnSetVarCmd::VpnSetVarCmd() : NexusCommand("vpn_setvar") { } int CommandListener::VpnSetVarCmd::runCommand(SocketClient *cli, char *data) { VpnController *vc = (VpnController *) NetworkManager::Instance()->findController("VPN"); char *bword; char *last; char varname[32]; char val[250]; if (!(bword = strtok_r(data, ":", &last))) goto out_inval; strncpy(varname, bword, sizeof(varname)); if (!(bword = strtok_r(NULL, ":", &last))) goto out_inval; strncpy(val, bword, sizeof(val)); if (!strcasecmp(varname, "vpn_gateway")) { if (vc->setVpnGateway(val)) goto out_inval; } else { cli->sendMsg(ErrorCode::CommandParameterError, "Variable not found.", true); return 0; } cli->sendMsg(ErrorCode::CommandOkay, "Variable written.", false); return 0; out_inval: errno = EINVAL; cli->sendMsg(ErrorCode::CommandParameterError, "Failed to set variable.", true); return 0; } CommandListener::VpnGetVarCmd::VpnGetVarCmd() : NexusCommand("vpn_getvar") { } int CommandListener::VpnGetVarCmd::runCommand(SocketClient *cli, char *data) { VpnController *vc = (VpnController *) NetworkManager::Instance()->findController("VPN"); char *bword; char *last; char varname[32]; if (!(bword = strtok_r(data, ":", &last))) goto out_inval; strncpy(varname, bword, sizeof(varname)); if (!strcasecmp(varname, "vpn_gateway")) { char buffer[255]; sprintf(buffer, "%s:%s", varname, inet_ntoa(vc->getVpnGateway())); cli->sendMsg(ErrorCode::VariableRead, buffer, false); } else { cli->sendMsg(ErrorCode::CommandParameterError, "Variable not found.", true); return 0; } cli->sendMsg(ErrorCode::CommandOkay, "Variable read.", false); return 0; out_inval: errno = EINVAL; cli->sendMsg(ErrorCode::CommandParameterError, "Failed to get variable.", true); return 0; } CommandListener::VpnDisableCmd::VpnDisableCmd() : NexusCommand("vpn_disable") { } Loading
nexus/CommandListener.h +14 −0 Original line number Diff line number Diff line Loading @@ -95,6 +95,20 @@ private: int runCommand(SocketClient *c, char *data); }; class VpnSetVarCmd : public NexusCommand { public: VpnSetVarCmd(); virtual ~VpnSetVarCmd() {} int runCommand(SocketClient *c, char *data); }; class VpnGetVarCmd : public NexusCommand { public: VpnGetVarCmd(); virtual ~VpnGetVarCmd() {} int runCommand(SocketClient *c, char *data); }; class VpnDisableCmd : public NexusCommand { public: VpnDisableCmd(); Loading
nexus/ErrorCode.h +3 −0 Original line number Diff line number Diff line Loading @@ -26,6 +26,9 @@ public: static const int WifiScanResult = 125; static const int WifiNetworkList = 126; static const int VariableRead = 127; static const int VariableWrite = 128; // 200 series - Requested action has been successfully completed static const int CommandOkay = 200; Loading
nexus/OpenVpnController.cpp +27 −53 Original line number Diff line number Diff line Loading @@ -14,17 +14,27 @@ * limitations under the License. */ #include <errno.h> #include <netinet/in.h> #include <arpa/inet.h> #define LOG_TAG "OpenVpnController" #include <cutils/log.h> #include <cutils/properties.h> #include <sysutils/ServiceManager.h> #include "OpenVpnController.h" #define DAEMON_PROP_NAME "vpn.openvpn.status" #define DAEMON_CONFIG_FILE "/data/misc/openvpn/openvpn.conf" OpenVpnController::OpenVpnController() : VpnController() { mServiceManager = new ServiceManager(); } OpenVpnController::~OpenVpnController() { delete mServiceManager; } int OpenVpnController::start() { Loading @@ -37,68 +47,32 @@ int OpenVpnController::stop() { int OpenVpnController::enable() { // Validate configuration file // Validate key file if (startServiceDaemon()) return -1; errno = -ENOSYS; if (validateConfig()) { LOGE("Error validating configuration file"); return -1; } int OpenVpnController::startServiceDaemon() { char status[PROPERTY_VALUE_MAX]; int count = 100; property_set("ctl.start", "openvpn"); sched_yield(); if (mServiceManager->start("openvpn")) return -1; while (count-- > 0) { if (property_get(DAEMON_PROP_NAME, status, NULL)) { if (strcmp(status, "ok") == 0) return 0; else if (strcmp(DAEMON_PROP_NAME, "failed") == 0) return -1; } usleep(200000); } property_set(DAEMON_PROP_NAME, "timeout"); return -1; } int OpenVpnController::stopServiceDaemon() { char status[PROPERTY_VALUE_MAX] = {'\0'}; int count = 50; int OpenVpnController::disable() { if (property_get(DAEMON_PROP_NAME, status, NULL) && !strcmp(status, "stopped")) { LOGD("Service already stopped"); if (mServiceManager->stop("openvpn")) return -1; return 0; } property_set("ctl.stop", "openvpn"); sched_yield(); while (count-- > 0) { if (property_get(DAEMON_PROP_NAME, status, NULL)) { if (!strcmp(status, "stopped")) break; } usleep(100000); } int OpenVpnController::validateConfig() { unlink(DAEMON_CONFIG_FILE); if (!count) { LOGD("Timed out waiting for openvpn to stop"); errno = ETIMEDOUT; FILE *fp = fopen(DAEMON_CONFIG_FILE, "w"); if (!fp) return -1; } fprintf(fp, "remote %s 1194\n", inet_ntoa(getVpnGateway())); fclose(fp); return 0; } int OpenVpnController::disable() { errno = -ENOSYS; return -1; }
nexus/OpenVpnController.h +6 −3 Original line number Diff line number Diff line Loading @@ -19,11 +19,15 @@ #include "VpnController.h" class ServiceManager; class OpenVpnController : public VpnController { private: ServiceManager *mServiceManager; public: OpenVpnController(); virtual ~OpenVpnController() {} virtual ~OpenVpnController(); int start(); int stop(); Loading @@ -33,8 +37,7 @@ public: protected: private: int startServiceDaemon(); int stopServiceDaemon(); int validateConfig(); }; #endif