4 * The secure anycast tunneling protocol (satp) defines a protocol used
5 * for communication between any combination of unicast and anycast
6 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
7 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
8 * ethernet, ip, arp ...). satp directly includes cryptography and
9 * message authentication based on the methodes used by SRTP. It is
10 * intended to deliver a generic, scaleable and secure solution for
11 * tunneling and relaying of packets of any protocol.
14 * Copyright (C) 2007-2009 Othmar Gsenger, Erwin Nindl,
15 * Christian Pointner <satp@wirdorange.org>
17 * This file is part of Anytun.
19 * Anytun is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
24 * Anytun is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with anytun. If not, see <http://www.gnu.org/licenses/>.
33 #ifndef ANYTUN_sysexec_hpp_INCLUDED
34 #define ANYTUN_sysexec_hpp_INCLUDED
37 #include <iostream> // todo remove
43 CloseHandle(process_info_.hProcess);
44 CloseHandle(process_info_.hThread);
48 STARTUPINFOA getStartupInfo()
50 STARTUPINFOA startup_info;
51 startup_info.cb = sizeof(STARTUPINFOA);
52 GetStartupInfoA(&startup_info);
54 //startup_info.dwFlags = STARTF_USESTDHANDLES;
55 //startup_info.hStdInput = CreateFile("NUL", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, 0, 0, 0); // INVALID_HANDLE_VALUE;
56 //startup_info.hStdOutput = CreateFile("NUL", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, 0, 0, 0); // INVALID_HANDLE_VALUE;
57 //startup_info.hStdError = CreateFile("NUL", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, 0, 0, 0); // INVALID_HANDLE_VALUE;
58 startup_info.dwFlags |= STARTF_USESHOWWINDOW;
59 startup_info.wShowWindow = SW_HIDE;
64 char const* const BATCH_FILE_EXTS[] = { ".bat", ".cmd" };
65 int const BATCH_FILE_EXTS_COUNT = sizeof(BATCH_FILE_EXTS) / sizeof(BATCH_FILE_EXTS[0]);
67 bool endsWith(std::string const& string, std::string const& suffix)
69 return string.find(suffix, string.size() - suffix.size()) != std::string::npos;
72 void SysExec::doExec(StringVector args, StringList env)
74 std::vector<char> arguments;
76 bool isBatchFile = false;
77 for(int i = 0; i < BATCH_FILE_EXTS_COUNT; ++i) {
78 if(endsWith(script_, BATCH_FILE_EXTS[i])) {
85 std::string const BATCH_INTERPRETER = "cmd /c \"";
86 arguments.insert(arguments.end(), BATCH_INTERPRETER.begin(), BATCH_INTERPRETER.end());
88 arguments.push_back('\"');
89 arguments.insert(arguments.end(), script_.begin(), script_.end());
90 arguments.push_back('\"');
91 arguments.push_back(' ');
93 for(StringVector::const_iterator it = args.begin(); it != args.end(); ++it) {
94 arguments.push_back('\"');
95 arguments.insert(arguments.end(), it->begin(), it->end());
96 arguments.push_back('\"');
97 arguments.push_back(' ');
101 arguments.push_back('\"');
103 arguments.push_back(0);
105 STARTUPINFOA startup_info = getStartupInfo();
107 std::map<std::string, std::string> envDict;
108 for(StringList::const_iterator it = env.begin(); it != env.begin(); ++it) {
109 size_t delimiter_pos = it->find('=');
110 envDict.insert(std::make_pair(it->substr(0, delimiter_pos), it->substr(delimiter_pos + 1)));
112 std::vector<char> env;
113 for(std::map<std::string, std::string>::iterator it = envDict.begin(); it != envDict.end(); ++it) {
114 env.insert(env.end(), it->first.begin(), it->first.end());
119 if(!CreateProcessA(NULL,
130 cLog.msg(Log::PRIO_ERROR) << "executing script '" << script_ << "' CreateProcess() error: " << GetLastError();
135 int SysExec::waitForScript()
137 DWORD result = WaitForSingleObject(process_info_.hProcess, INFINITE);
138 assert(WAIT_OBJECT_0 == result); // WAIT_FAILED, WAIT_TIMEOUT ... ???
139 bool success = GetExitCodeProcess(process_info_.hProcess, &return_code_) != 0;
140 assert(true == success); // false -> HU?
142 CloseHandle(process_info_.hProcess);
143 CloseHandle(process_info_.hThread);
146 return static_cast<int>(return_code_);
149 void SysExec::waitAndDestroy(SysExec*& s)
156 cLog.msg(Log::PRIO_NOTICE) << "script '" << s->script_ << "' returned " << s->return_code_;
162 #endif // ANYTUN_sysexec_hpp_INCLUDED