| 1 | /**************************************************************************
|
|---|
| 2 | * Copyright (C) 2004-2007 by Michael Medin <michael@medin.name> *
|
|---|
| 3 | * *
|
|---|
| 4 | * This code is part of NSClient++ - http://trac.nakednuns.org/nscp *
|
|---|
| 5 | * *
|
|---|
| 6 | * This program is free software; you can redistribute it and/or modify *
|
|---|
| 7 | * it under the terms of the GNU General Public License as published by *
|
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or *
|
|---|
| 9 | * (at your option) any later version. *
|
|---|
| 10 | * *
|
|---|
| 11 | * This program is distributed in the hope that it will be useful, *
|
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|---|
| 14 | * GNU General Public License for more details. *
|
|---|
| 15 | * *
|
|---|
| 16 | * You should have received a copy of the GNU General Public License *
|
|---|
| 17 | * along with this program; if not, write to the *
|
|---|
| 18 | * Free Software Foundation, Inc., *
|
|---|
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|---|
| 20 | ***************************************************************************/
|
|---|
| 21 |
|
|---|
| 22 | #include <boost/foreach.hpp>
|
|---|
| 23 | #include <boost/tokenizer.hpp>
|
|---|
| 24 |
|
|---|
| 25 | #include <strEx.h>
|
|---|
| 26 | #include <arrayBuffer.h>
|
|---|
| 27 |
|
|---|
| 28 | #include <nscapi/nscapi_core_wrapper.hpp>
|
|---|
| 29 | #include <nscapi/nscapi_plugin_wrapper.hpp>
|
|---|
| 30 | #include <nscapi/functions.hpp>
|
|---|
| 31 | #include <settings/macros.h>
|
|---|
| 32 |
|
|---|
| 33 | #include "../libs/protobuf/plugin.proto.h"
|
|---|
| 34 | #include "../libs/protobuf/log.proto.h"
|
|---|
| 35 |
|
|---|
| 36 | using namespace nscp::helpers;
|
|---|
| 37 |
|
|---|
| 38 | #define CORE_LOG_ERROR_STD(msg) CORE_LOG_ERROR(((std::wstring)msg).c_str())
|
|---|
| 39 | #define CORE_LOG_ERROR(msg) CORE_ANY_MSG(msg,NSCAPI::error)
|
|---|
| 40 |
|
|---|
| 41 | #define CORE_LOG_CRITICAL_STD(msg) CORE_LOG_CRITICAL(((std::wstring)msg).c_str())
|
|---|
| 42 | #define CORE_LOG_CRITICAL(msg) CORE_ANY_MSG(msg,NSCAPI::critical)
|
|---|
| 43 |
|
|---|
| 44 | #define CORE_LOG_MESSAGE_STD(msg) CORE_LOG_MESSAGE(((std::wstring)msg).c_str())
|
|---|
| 45 | #define CORE_LOG_MESSAGE(msg) CORE_ANY_MSG(msg,NSCAPI::log)
|
|---|
| 46 |
|
|---|
| 47 | #define CORE_DEBUG_MSG_STD(msg) CORE_DEBUG_MSG((std::wstring)msg)
|
|---|
| 48 | #define CORE_DEBUG_MSG(msg) CORE_ANY_MSG(msg,NSCAPI::debug)
|
|---|
| 49 |
|
|---|
| 50 | #define CORE_ANY_MSG(msg, type) Message(type, __FILE__, __LINE__, msg)
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 54 | // Callbacks into the core
|
|---|
| 55 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 56 |
|
|---|
| 57 | /**
|
|---|
| 58 | * Callback to send a message through to the core
|
|---|
| 59 | *
|
|---|
| 60 | * @param msgType Message type (debug, warning, etc.)
|
|---|
| 61 | * @param file File where message was generated (__FILE__)
|
|---|
| 62 | * @param line Line where message was generated (__LINE__)
|
|---|
| 63 | * @param message Message in human readable format
|
|---|
| 64 | * @throws nscapi::nscapi_exception When core pointer set is unavailable.
|
|---|
| 65 | */
|
|---|
| 66 | void nscapi::core_wrapper::Message(int msgType, std::string file, int line, std::wstring logMessage) {
|
|---|
| 67 | if (fNSAPIMessage) {
|
|---|
| 68 | if ((msgType == NSCAPI::debug) && (!logDebug()))
|
|---|
| 69 | return;
|
|---|
| 70 | std::string str;
|
|---|
| 71 | try {
|
|---|
| 72 | LogMessage::LogMessage message;
|
|---|
| 73 | LogMessage::Message *msg = message.add_message();
|
|---|
| 74 | msg->set_level(nscapi::functions::log_to_gpb(msgType));
|
|---|
| 75 | msg->set_file(file);
|
|---|
| 76 | msg->set_line(line);
|
|---|
| 77 | msg->set_message(to_string(logMessage));
|
|---|
| 78 | if (!message.SerializeToString(&str)) {
|
|---|
| 79 | std::cout << "Failed to generate message";
|
|---|
| 80 | }
|
|---|
| 81 | return fNSAPIMessage(str.c_str(), str.size());
|
|---|
| 82 | } catch (...) {
|
|---|
| 83 | std::wcout << _T("Failed to generate message: ");
|
|---|
| 84 | }
|
|---|
| 85 | // return fNSAPIMessage(to_string(logMessage).c_str(), logMessage.size());
|
|---|
| 86 | }
|
|---|
| 87 | else
|
|---|
| 88 | std::wcout << _T("*** *** *** NSCore not loaded, dumping log: ") << to_wstring(file) << _T(":") << line << _T(": ") << std::endl << logMessage << std::endl;
|
|---|
| 89 | }
|
|---|
| 90 | /**
|
|---|
| 91 | * Inject a request command in the core (this will then be sent to the plug-in stack for processing)
|
|---|
| 92 | * @param command Command to inject (password should not be included.
|
|---|
| 93 | * @return The result (if any) of the command.
|
|---|
| 94 | * @throws nscapi::nscapi_exception When core pointer set is unavailable or an unknown inject error occurs.
|
|---|
| 95 | */
|
|---|
| 96 |
|
|---|
| 97 | /**
|
|---|
| 98 | * Inject a request command in the core (this will then be sent to the plug-in stack for processing)
|
|---|
| 99 | * @param command Command to inject
|
|---|
| 100 | * @param argLen The length of the argument buffer
|
|---|
| 101 | * @param **argument The argument buffer
|
|---|
| 102 | * @param *returnMessageBuffer Buffer to hold the returned message
|
|---|
| 103 | * @param returnMessageBufferLen Length of returnMessageBuffer
|
|---|
| 104 | * @param *returnPerfBuffer Buffer to hold the returned performance data
|
|---|
| 105 | * @param returnPerfBufferLen returnPerfBuffer
|
|---|
| 106 | * @return The returned status of the command
|
|---|
| 107 | */
|
|---|
| 108 | NSCAPI::nagiosReturn nscapi::core_wrapper::InjectCommandRAW(const wchar_t* command, const char *request, const unsigned int request_len, char **response, unsigned int *response_len)
|
|---|
| 109 | {
|
|---|
| 110 | if (!fNSAPIInject)
|
|---|
| 111 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 112 | return fNSAPIInject(command, request, request_len, response, response_len);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | void nscapi::core_wrapper::DestroyBuffer(char**buffer) {
|
|---|
| 116 | if (!fNSAPIDestroyBuffer)
|
|---|
| 117 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 118 | return fNSAPIDestroyBuffer(buffer);
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | NSCAPI::errorReturn nscapi::core_wrapper::NotifyChannel(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn code, std::string result) {
|
|---|
| 122 | if (!fNSAPINotify)
|
|---|
| 123 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 124 | return fNSAPINotify(channel.c_str(), command.c_str(), code, result.c_str(), result.size());
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | /**
|
|---|
| 128 | * Inject a request command in the core (this will then be sent to the plug-in stack for processing)
|
|---|
| 129 | * @param command Command to inject (password should not be included.
|
|---|
| 130 | * @param argLen The length of the argument buffer
|
|---|
| 131 | * @param **argument The argument buffer
|
|---|
| 132 | * @param message The return message buffer
|
|---|
| 133 | * @param perf The return performance data buffer
|
|---|
| 134 | * @return The return of the command
|
|---|
| 135 | */
|
|---|
| 136 | NSCAPI::nagiosReturn nscapi::core_wrapper::InjectSimpleCommand(const std::wstring command, const std::list<std::wstring> argument, std::wstring & msg, std::wstring & perf)
|
|---|
| 137 | {
|
|---|
| 138 | if (!fNSAPIInject)
|
|---|
| 139 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 140 |
|
|---|
| 141 | std::string response;
|
|---|
| 142 | NSCAPI::nagiosReturn ret = InjectCommand(command, argument, response);
|
|---|
| 143 | if (!response.empty()) {
|
|---|
| 144 | PluginCommand::ResponseMessage rsp_msg;
|
|---|
| 145 | rsp_msg.ParseFromString(response);
|
|---|
| 146 | if (rsp_msg.payload_size() != 1) {
|
|---|
| 147 | CORE_LOG_ERROR_STD(_T("Failed to extract return message not 1 payload: ") + strEx::itos(rsp_msg.payload_size()));
|
|---|
| 148 | return NSCAPI::returnUNKNOWN;
|
|---|
| 149 | }
|
|---|
| 150 | ::PluginCommand::Response payload = rsp_msg.payload(0);
|
|---|
| 151 | msg = utf8::cvt<std::wstring>(payload.message());
|
|---|
| 152 | perf = utf8::cvt<std::wstring>(::nscapi::functions::build_performance_data(payload));
|
|---|
| 153 | }
|
|---|
| 154 | return ret;
|
|---|
| 155 | }
|
|---|
| 156 | /**
|
|---|
| 157 | * Inject a request command in the core (this will then be sent to the plug-in stack for processing)
|
|---|
| 158 | * @param command Command to inject (password should not be included.
|
|---|
| 159 | * @param argLen The length of the argument buffer
|
|---|
| 160 | * @param **argument The argument buffer
|
|---|
| 161 | * @param message The return message buffer
|
|---|
| 162 | * @param perf The return performance data buffer
|
|---|
| 163 | * @return The return of the command
|
|---|
| 164 | */
|
|---|
| 165 | NSCAPI::nagiosReturn nscapi::core_wrapper::InjectCommand(const std::wstring command, const std::list<std::wstring> argument, std::string & result)
|
|---|
| 166 | {
|
|---|
| 167 | if (!fNSAPIInject)
|
|---|
| 168 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 | PluginCommand::RequestMessage message;
|
|---|
| 172 | PluginCommand::Header *hdr = message.mutable_header();
|
|---|
| 173 | hdr->set_type(PluginCommand::Header_Type_REQUEST);
|
|---|
| 174 | hdr->set_version(PluginCommand::Header_Version_VERSION_1);
|
|---|
| 175 |
|
|---|
| 176 | PluginCommand::Request *req = message.add_payload();
|
|---|
| 177 | req->set_command(to_string(command));
|
|---|
| 178 | req->set_version(PluginCommand::Request_Version_VERSION_1);
|
|---|
| 179 |
|
|---|
| 180 | BOOST_FOREACH(std::wstring s, argument)
|
|---|
| 181 | req->add_arguments(to_string(s));
|
|---|
| 182 |
|
|---|
| 183 | std::string request;
|
|---|
| 184 | message.SerializeToString(&request);
|
|---|
| 185 |
|
|---|
| 186 | char *buffer = NULL;
|
|---|
| 187 | unsigned int buffer_size = 0;
|
|---|
| 188 |
|
|---|
| 189 | NSCAPI::nagiosReturn retC = InjectCommandRAW(command.c_str(), request.c_str(), request.size(), &buffer, &buffer_size);
|
|---|
| 190 |
|
|---|
| 191 | if (buffer_size > 0 && buffer != NULL) {
|
|---|
| 192 | //PluginCommand::ResponseMessage rsp_msg;
|
|---|
| 193 | result = std::string(buffer, buffer_size);
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | DestroyBuffer(&buffer);
|
|---|
| 197 | switch (retC) {
|
|---|
| 198 | case NSCAPI::returnIgnored:
|
|---|
| 199 | CORE_LOG_MESSAGE_STD(_T("No handler for command '") + command + _T("'."));
|
|---|
| 200 | break;
|
|---|
| 201 | case NSCAPI::returnOK:
|
|---|
| 202 | case NSCAPI::returnCRIT:
|
|---|
| 203 | case NSCAPI::returnWARN:
|
|---|
| 204 | case NSCAPI::returnUNKNOWN:
|
|---|
| 205 | break;
|
|---|
| 206 | default:
|
|---|
| 207 | throw nscapi::nscapi_exception(_T("Unknown return code when injecting: ") + std::wstring(command));
|
|---|
| 208 | }
|
|---|
| 209 | return retC;
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | /**
|
|---|
| 213 | * A wrapper around the InjetCommand that is simpler to use.
|
|---|
| 214 | * Parses a string by splitting and makes the array and also manages return buffers and such.
|
|---|
| 215 | * @param command The command to execute
|
|---|
| 216 | * @param buffer The buffer to split
|
|---|
| 217 | * @param spliwchar_t The char to use as splitter
|
|---|
| 218 | * @param message The return message buffer
|
|---|
| 219 | * @param perf The return performance data buffer
|
|---|
| 220 | * @return The result of the command
|
|---|
| 221 | */
|
|---|
| 222 | NSCAPI::nagiosReturn nscapi::core_wrapper::InjectSplitAndCommand(const wchar_t* command, wchar_t* buffer, wchar_t splitChar, std::wstring & message, std::wstring & perf)
|
|---|
| 223 | {
|
|---|
| 224 | if (!fNSAPIInject)
|
|---|
| 225 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 226 |
|
|---|
| 227 | std::wstring args = std::wstring(buffer);
|
|---|
| 228 | boost::tokenizer<boost::escaped_list_separator<wchar_t>, std::wstring::const_iterator, std::wstring > tok(args, boost::escaped_list_separator<wchar_t>(L'\\', splitChar, L'\"'));
|
|---|
| 229 | std::list<std::wstring> arglist;
|
|---|
| 230 | BOOST_FOREACH(std::wstring s, tok)
|
|---|
| 231 | arglist.push_back(s);
|
|---|
| 232 | return InjectSimpleCommand(command, arglist, message, perf);
|
|---|
| 233 | }
|
|---|
| 234 | /**
|
|---|
| 235 | * A wrapper around the InjetCommand that is simpler to use.
|
|---|
| 236 | * @param command The command to execute
|
|---|
| 237 | * @param buffer The buffer to split
|
|---|
| 238 | * @param spliwchar_t The char to use as splitter
|
|---|
| 239 | * @param message The return message buffer
|
|---|
| 240 | * @param perf The return performance data buffer
|
|---|
| 241 | * @return The result of the command
|
|---|
| 242 | */
|
|---|
| 243 | NSCAPI::nagiosReturn nscapi::core_wrapper::InjectSplitAndCommand(const std::wstring command, const std::wstring buffer, wchar_t spliwchar_t, std::wstring & message, std::wstring & perf, bool escape) {
|
|---|
| 244 | if (!fNSAPIInject)
|
|---|
| 245 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 246 | std::list<std::wstring> arglist;
|
|---|
| 247 | if (escape) {
|
|---|
| 248 | boost::tokenizer<boost::escaped_list_separator<wchar_t>, std::wstring::const_iterator, std::wstring > tok(buffer, boost::escaped_list_separator<wchar_t>(L'\\', spliwchar_t, L'\"'));
|
|---|
| 249 | BOOST_FOREACH(std::wstring s, tok)
|
|---|
| 250 | arglist.push_back(s);
|
|---|
| 251 | } else {
|
|---|
| 252 | std::wstring split;
|
|---|
| 253 | split.push_back(spliwchar_t);
|
|---|
| 254 | boost::tokenizer<boost::escaped_list_separator<wchar_t>, std::wstring::const_iterator, std::wstring > tok(buffer, boost::escaped_list_separator<wchar_t>(_T(""), split, _T("\"")));
|
|---|
| 255 | BOOST_FOREACH(std::wstring s, tok)
|
|---|
| 256 | arglist.push_back(s);
|
|---|
| 257 | }
|
|---|
| 258 | return InjectSimpleCommand(command.c_str(), arglist, message, perf);
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | NSCAPI::nagiosReturn nscapi::core_wrapper::InjectNRPECommand(const std::wstring command, const std::wstring buffer, std::wstring & message, std::wstring & perf) {
|
|---|
| 262 | if (!fNSAPIInject)
|
|---|
| 263 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 264 | boost::tokenizer<boost::char_separator<wchar_t>, std::wstring::const_iterator, std::wstring > tok(buffer, boost::char_separator<wchar_t>(_T("!")));
|
|---|
| 265 | std::list<std::wstring> arglist;
|
|---|
| 266 | BOOST_FOREACH(std::wstring s, tok)
|
|---|
| 267 | arglist.push_back(s);
|
|---|
| 268 | return InjectSimpleCommand(command.c_str(), arglist, message, perf);
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 | /**
|
|---|
| 273 | * Ask the core to shutdown (only works when run as a service, o/w does nothing ?
|
|---|
| 274 | * @todo Check if this might cause damage if not run as a service.
|
|---|
| 275 | */
|
|---|
| 276 | void nscapi::core_wrapper::StopService(void) {
|
|---|
| 277 | if (fNSAPIStopServer)
|
|---|
| 278 | fNSAPIStopServer();
|
|---|
| 279 | }
|
|---|
| 280 | /**
|
|---|
| 281 | * Close the program (usefull for tray/testmode) without stopping the service (unless this is the service).
|
|---|
| 282 | * @author mickem
|
|---|
| 283 | */
|
|---|
| 284 | void nscapi::core_wrapper::Exit(void) {
|
|---|
| 285 | if (fNSAPIExit)
|
|---|
| 286 | fNSAPIExit();
|
|---|
| 287 | }
|
|---|
| 288 | /**
|
|---|
| 289 | * Retrieve a string from the settings subsystem (INI-file)
|
|---|
| 290 | * Might possibly be located in the registry in the future.
|
|---|
| 291 | *
|
|---|
| 292 | * @param section Section key (generally module specific, make sure this is "unique")
|
|---|
| 293 | * @param key The key to retrieve
|
|---|
| 294 | * @param defaultValue A default value (if no value is set in the settings file)
|
|---|
| 295 | * @return the current value or defaultValue if no value is set.
|
|---|
| 296 | * @throws nscapi::nscapi_exception When core pointer set is unavailable or an error occurs.
|
|---|
| 297 | */
|
|---|
| 298 | std::wstring nscapi::core_wrapper::getSettingsString(std::wstring section, std::wstring key, std::wstring defaultValue) {
|
|---|
| 299 | if (!fNSAPIGetSettingsString)
|
|---|
| 300 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 301 | unsigned int buf_len = getBufferLength();
|
|---|
| 302 | wchar_t *buffer = new wchar_t[buf_len+1];
|
|---|
| 303 | if (fNSAPIGetSettingsString(section.c_str(), key.c_str(), defaultValue.c_str(), buffer, buf_len) != NSCAPI::isSuccess) {
|
|---|
| 304 | delete [] buffer;
|
|---|
| 305 | throw nscapi::nscapi_exception(_T("Settings could not be retrieved."));
|
|---|
| 306 | }
|
|---|
| 307 | std::wstring ret = buffer;
|
|---|
| 308 | delete [] buffer;
|
|---|
| 309 | return ret;
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | std::wstring nscapi::core_wrapper::expand_path(std::wstring value) {
|
|---|
| 313 | if (!fNSAPIExpandPath)
|
|---|
| 314 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 315 | unsigned int buf_len = getBufferLength();
|
|---|
| 316 | wchar_t *buffer = new wchar_t[buf_len+1];
|
|---|
| 317 | if (fNSAPIExpandPath(value.c_str(), buffer, buf_len) != NSCAPI::isSuccess) {
|
|---|
| 318 | delete [] buffer;
|
|---|
| 319 | throw nscapi::nscapi_exception(_T("Settings could not be retrieved."));
|
|---|
| 320 | }
|
|---|
| 321 | std::wstring ret = buffer;
|
|---|
| 322 | delete [] buffer;
|
|---|
| 323 | return ret;
|
|---|
| 324 | }
|
|---|
| 325 | /**
|
|---|
| 326 | * Get a section of settings strings
|
|---|
| 327 | * @param section The section to retrieve
|
|---|
| 328 | * @return The keys in the section
|
|---|
| 329 | */
|
|---|
| 330 | std::list<std::wstring> nscapi::core_wrapper::getSettingsSection(std::wstring section) {
|
|---|
| 331 | if (!fNSAPIGetSettingsSection)
|
|---|
| 332 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 333 | array_buffer::arrayBuffer aBuffer = NULL;
|
|---|
| 334 | unsigned int argLen = 0;
|
|---|
| 335 | if (fNSAPIGetSettingsSection(section.c_str(), &aBuffer, &argLen) != NSCAPI::isSuccess) {
|
|---|
| 336 | throw nscapi::nscapi_exception(_T("Settings could not be retrieved."));
|
|---|
| 337 | }
|
|---|
| 338 | std::list<std::wstring> ret = array_buffer::arrayBuffer2list(argLen, aBuffer);
|
|---|
| 339 | if (fNSAPIReleaseSettingsSectionBuffer(&aBuffer, &argLen) != NSCAPI::isSuccess) {
|
|---|
| 340 | throw nscapi::nscapi_exception(_T("Settings could not be destroyed."));
|
|---|
| 341 | }
|
|---|
| 342 | if (aBuffer != NULL)
|
|---|
| 343 | throw nscapi::nscapi_exception(_T("buffer is not null?."));
|
|---|
| 344 | return ret;
|
|---|
| 345 | }
|
|---|
| 346 | /**
|
|---|
| 347 | * Retrieve an int from the settings subsystem (INI-file)
|
|---|
| 348 | * Might possibly be located in the registry in the future.
|
|---|
| 349 | *
|
|---|
| 350 | * @param section Section key (generally module specific, make sure this is "unique")
|
|---|
| 351 | * @param key The key to retrieve
|
|---|
| 352 | * @param defaultValue A default value (if no value is set in the settings file)
|
|---|
| 353 | * @return the current value or defaultValue if no value is set.
|
|---|
| 354 | * @throws nscapi::nscapi_exception When core pointer set is unavailable.
|
|---|
| 355 | */
|
|---|
| 356 | int nscapi::core_wrapper::getSettingsInt(std::wstring section, std::wstring key, int defaultValue) {
|
|---|
| 357 | if (!fNSAPIGetSettingsInt)
|
|---|
| 358 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 359 | return fNSAPIGetSettingsInt(section.c_str(), key.c_str(), defaultValue);
|
|---|
| 360 | }
|
|---|
| 361 | bool nscapi::core_wrapper::getSettingsBool(std::wstring section, std::wstring key, bool defaultValue) {
|
|---|
| 362 | if (!fNSAPIGetSettingsBool)
|
|---|
| 363 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 364 | return fNSAPIGetSettingsBool(section.c_str(), key.c_str(), defaultValue?1:0) == 1;
|
|---|
| 365 | }
|
|---|
| 366 | void nscapi::core_wrapper::settings_register_key(std::wstring path, std::wstring key, NSCAPI::settings_type type, std::wstring title, std::wstring description, std::wstring defaultValue, bool advanced) {
|
|---|
| 367 | if (!fNSAPISettingsRegKey)
|
|---|
| 368 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 369 | fNSAPISettingsRegKey(path.c_str(), key.c_str(), type, title.c_str(), description.c_str(), defaultValue.c_str(), advanced);
|
|---|
| 370 | }
|
|---|
| 371 | void nscapi::core_wrapper::settings_register_path(std::wstring path, std::wstring title, std::wstring description, bool advanced) {
|
|---|
| 372 | if (!fNSAPISettingsRegPath)
|
|---|
| 373 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 374 | fNSAPISettingsRegPath(path.c_str(), title.c_str(), description.c_str(), advanced);
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 |
|
|---|
| 378 | void nscapi::core_wrapper::settings_save() {
|
|---|
| 379 | if (!fNSAPISettingsSave)
|
|---|
| 380 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 381 | fNSAPISettingsSave();
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| 384 | /**
|
|---|
| 385 | * Retrieve the application name (in human readable format) from the core.
|
|---|
| 386 | * @return A string representing the application name.
|
|---|
| 387 | * @throws nscapi::nscapi_exception When core pointer set is unavailable or an unexpected error occurs.
|
|---|
| 388 | */
|
|---|
| 389 | std::wstring nscapi::core_wrapper::getApplicationName() {
|
|---|
| 390 | if (!fNSAPIGetApplicationName)
|
|---|
| 391 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 392 | unsigned int buf_len = getBufferLength();
|
|---|
| 393 | wchar_t *buffer = new wchar_t[buf_len+1];
|
|---|
| 394 | if (fNSAPIGetApplicationName(buffer, buf_len) != NSCAPI::isSuccess) {
|
|---|
| 395 | delete [] buffer;
|
|---|
| 396 | throw nscapi::nscapi_exception(_T("Application name could not be retrieved"));
|
|---|
| 397 | }
|
|---|
| 398 | std::wstring ret = buffer;
|
|---|
| 399 | delete [] buffer;
|
|---|
| 400 | return ret;
|
|---|
| 401 | }
|
|---|
| 402 | /**
|
|---|
| 403 | * Retrieve the directory root of the application from the core.
|
|---|
| 404 | * @return A string representing the base path.
|
|---|
| 405 | * @throws nscapi::nscapi_exception When core pointer set is unavailable or an unexpected error occurs.
|
|---|
| 406 | */
|
|---|
| 407 | std::wstring nscapi::core_wrapper::getBasePath() {
|
|---|
| 408 | if (!fNSAPIGetBasePath)
|
|---|
| 409 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 410 | unsigned int buf_len = getBufferLength();
|
|---|
| 411 | wchar_t *buffer = new wchar_t[buf_len+1];
|
|---|
| 412 | if (fNSAPIGetBasePath(buffer, buf_len) != NSCAPI::isSuccess) {
|
|---|
| 413 | delete [] buffer;
|
|---|
| 414 | throw nscapi::nscapi_exception(_T("Base path could not be retrieved"));
|
|---|
| 415 | }
|
|---|
| 416 | std::wstring ret = buffer;
|
|---|
| 417 | delete [] buffer;
|
|---|
| 418 | return ret;
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | unsigned int nscapi::core_wrapper::getBufferLength() {
|
|---|
| 422 | static unsigned int len = 0;
|
|---|
| 423 | if (len == 0) {
|
|---|
| 424 | len = getSettingsInt(setting_keys::settings_def::PAYLOAD_LEN_PATH, setting_keys::settings_def::PAYLOAD_LEN, setting_keys::settings_def::PAYLOAD_LEN_DEFAULT);
|
|---|
| 425 | }
|
|---|
| 426 | return len;
|
|---|
| 427 | }
|
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 | bool nscapi::core_wrapper::logDebug() {
|
|---|
| 431 | enum status {unknown, debug, nodebug };
|
|---|
| 432 | static status d = unknown;
|
|---|
| 433 | if (d == unknown) {
|
|---|
| 434 | if (checkLogMessages(debug)== NSCAPI::istrue)
|
|---|
| 435 | d = debug;
|
|---|
| 436 | else
|
|---|
| 437 | d = nodebug;
|
|---|
| 438 | }
|
|---|
| 439 | return (d == debug);
|
|---|
| 440 | }
|
|---|
| 441 |
|
|---|
| 442 | std::wstring nscapi::core_wrapper::Encrypt(std::wstring str, unsigned int algorithm) {
|
|---|
| 443 | if (!fNSAPIEncrypt)
|
|---|
| 444 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 445 | unsigned int len = 0;
|
|---|
| 446 | // @todo investigate potential problems with static_cast<unsigned int>
|
|---|
| 447 | fNSAPIEncrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), NULL, &len);
|
|---|
| 448 | len+=2;
|
|---|
| 449 | wchar_t *buf = new wchar_t[len+1];
|
|---|
| 450 | NSCAPI::errorReturn ret = fNSAPIEncrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), buf, &len);
|
|---|
| 451 | if (ret == NSCAPI::isSuccess) {
|
|---|
| 452 | std::wstring ret = buf;
|
|---|
| 453 | delete [] buf;
|
|---|
| 454 | return ret;
|
|---|
| 455 | }
|
|---|
| 456 | return _T("");
|
|---|
| 457 | }
|
|---|
| 458 | std::wstring nscapi::core_wrapper::Decrypt(std::wstring str, unsigned int algorithm) {
|
|---|
| 459 | if (!fNSAPIDecrypt)
|
|---|
| 460 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 461 | unsigned int len = 0;
|
|---|
| 462 | // @todo investigate potential problems with: static_cast<unsigned int>(str.size())
|
|---|
| 463 | fNSAPIDecrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), NULL, &len);
|
|---|
| 464 | len+=2;
|
|---|
| 465 | wchar_t *buf = new wchar_t[len+1];
|
|---|
| 466 | NSCAPI::errorReturn ret = fNSAPIDecrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), buf, &len);
|
|---|
| 467 | if (ret == NSCAPI::isSuccess) {
|
|---|
| 468 | std::wstring ret = buf;
|
|---|
| 469 | delete [] buf;
|
|---|
| 470 | return ret;
|
|---|
| 471 | }
|
|---|
| 472 | return _T("");
|
|---|
| 473 | }
|
|---|
| 474 | NSCAPI::errorReturn nscapi::core_wrapper::SetSettingsString(std::wstring section, std::wstring key, std::wstring value) {
|
|---|
| 475 | if (!fNSAPISetSettingsString)
|
|---|
| 476 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 477 | return fNSAPISetSettingsString(section.c_str(), key.c_str(), value.c_str());
|
|---|
| 478 | }
|
|---|
| 479 | NSCAPI::errorReturn nscapi::core_wrapper::SetSettingsInt(std::wstring section, std::wstring key, int value) {
|
|---|
| 480 | if (!fNSAPISetSettingsInt)
|
|---|
| 481 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 482 | return fNSAPISetSettingsInt(section.c_str(), key.c_str(), value);
|
|---|
| 483 | }
|
|---|
| 484 | NSCAPI::errorReturn nscapi::core_wrapper::WriteSettings(int type) {
|
|---|
| 485 | if (!fNSAPIWriteSettings)
|
|---|
| 486 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 487 | return fNSAPIWriteSettings(type);
|
|---|
| 488 | }
|
|---|
| 489 | NSCAPI::errorReturn nscapi::core_wrapper::ReadSettings(int type) {
|
|---|
| 490 | if (!fNSAPIReadSettings)
|
|---|
| 491 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 492 | return fNSAPIReadSettings(type);
|
|---|
| 493 | }
|
|---|
| 494 | NSCAPI::errorReturn nscapi::core_wrapper::Rehash(int flag) {
|
|---|
| 495 | if (!fNSAPIRehash)
|
|---|
| 496 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 497 | return fNSAPIRehash(flag);
|
|---|
| 498 | }
|
|---|
| 499 | nscapi::core_wrapper::plugin_info_list nscapi::core_wrapper::getPluginList() {
|
|---|
| 500 | if (!fNSAPIGetPluginList || !fNSAPIReleasePluginList)
|
|---|
| 501 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 502 | plugin_info_list ret;
|
|---|
| 503 |
|
|---|
| 504 |
|
|---|
| 505 | int len = 0;
|
|---|
| 506 | //NSCAPI::plugin_info_list **list2;
|
|---|
| 507 | //NSCAPI::plugin_info_list *list[1];
|
|---|
| 508 | NSCAPI::plugin_info *list[1];
|
|---|
| 509 | //typedef NSCAPI::errorReturn (*lpNSAPIGetPluginList)(int *len, NSAPI_plugin_info** list);
|
|---|
| 510 | //typedef NSCAPI::errorReturn (*lpNSAPIReleasePluginList)(int len, NSAPI_plugin_info** list);
|
|---|
| 511 | NSCAPI::errorReturn err = fNSAPIGetPluginList(&len, list);
|
|---|
| 512 | if (err != NSCAPI::isSuccess)
|
|---|
| 513 | return ret;
|
|---|
| 514 | for (int i=0;i<len;i++) {
|
|---|
| 515 | plugin_info_type info;
|
|---|
| 516 | info.description = (*list)[i].description;
|
|---|
| 517 | info.name = (*list)[i].name;
|
|---|
| 518 | info.dll = (*list)[i].dll;
|
|---|
| 519 | ret.push_back(info);
|
|---|
| 520 | }
|
|---|
| 521 | fNSAPIReleasePluginList(len, list);
|
|---|
| 522 | return ret;
|
|---|
| 523 | }
|
|---|
| 524 |
|
|---|
| 525 | std::list<std::wstring> nscapi::core_wrapper::getAllCommandNames() {
|
|---|
| 526 | if (!fNSAPIGetAllCommandNames || !fNSAPIReleaseAllCommandNamessBuffer )
|
|---|
| 527 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 528 | array_buffer::arrayBuffer aBuffer = NULL;
|
|---|
| 529 | unsigned int argLen = 0;
|
|---|
| 530 | if (fNSAPIGetAllCommandNames(&aBuffer, &argLen) != NSCAPI::isSuccess) {
|
|---|
| 531 | throw nscapi::nscapi_exception(_T("Commands could not be retrieved."));
|
|---|
| 532 | }
|
|---|
| 533 | std::list<std::wstring> ret = array_buffer::arrayBuffer2list(argLen, aBuffer);
|
|---|
| 534 | if (fNSAPIReleaseAllCommandNamessBuffer(&aBuffer, &argLen) != NSCAPI::isSuccess) {
|
|---|
| 535 | throw nscapi::nscapi_exception(_T("Commands could not be destroyed."));
|
|---|
| 536 | }
|
|---|
| 537 | if (aBuffer != NULL)
|
|---|
| 538 | throw nscapi::nscapi_exception(_T("buffer is not null?."));
|
|---|
| 539 | return ret;
|
|---|
| 540 | }
|
|---|
| 541 | std::wstring nscapi::core_wrapper::describeCommand(std::wstring command) {
|
|---|
| 542 | if (!fNSAPIDescribeCommand)
|
|---|
| 543 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 544 | unsigned int buf_len = getBufferLength();
|
|---|
| 545 | wchar_t *buffer = new wchar_t[buf_len+1];
|
|---|
| 546 | if (fNSAPIDescribeCommand(command.c_str(), buffer, buf_len) != NSCAPI::isSuccess) {
|
|---|
| 547 | delete [] buffer;
|
|---|
| 548 | throw nscapi::nscapi_exception(_T("Base path could not be retrieved"));
|
|---|
| 549 | }
|
|---|
| 550 | std::wstring ret = buffer;
|
|---|
| 551 | delete [] buffer;
|
|---|
| 552 | return ret;
|
|---|
| 553 | }
|
|---|
| 554 | void nscapi::core_wrapper::registerCommand(std::wstring command, std::wstring description) {
|
|---|
| 555 | if (!fNSAPIRegisterCommand)
|
|---|
| 556 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 557 | if (fNSAPIRegisterCommand(id_, command.c_str(), description.c_str()) != NSCAPI::isSuccess) {
|
|---|
| 558 | CORE_LOG_ERROR_STD(_T("Failed to register command: ") + command + _T(" in plugin: ") + to_wstring(id_));
|
|---|
| 559 | }
|
|---|
| 560 | }
|
|---|
| 561 |
|
|---|
| 562 |
|
|---|
| 563 | bool nscapi::core_wrapper::checkLogMessages(int type) {
|
|---|
| 564 | if (!fNSAPICheckLogMessages)
|
|---|
| 565 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 566 | return fNSAPICheckLogMessages(type) == NSCAPI::istrue;
|
|---|
| 567 | }
|
|---|
| 568 | /**
|
|---|
| 569 | * Retrieve the application version as a string (in human readable format) from the core.
|
|---|
| 570 | * @return A string representing the application version.
|
|---|
| 571 | * @throws nscapi::nscapi_exception When core pointer set is unavailable.
|
|---|
| 572 | */
|
|---|
| 573 | std::wstring nscapi::core_wrapper::getApplicationVersionString() {
|
|---|
| 574 | if (!fNSAPIGetApplicationVersionStr)
|
|---|
| 575 | throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
|
|---|
| 576 | unsigned int buf_len = getBufferLength();
|
|---|
| 577 | wchar_t *buffer = new wchar_t[buf_len+1];
|
|---|
| 578 | if (fNSAPIGetApplicationVersionStr(buffer, buf_len) != NSCAPI::isSuccess) {
|
|---|
| 579 | delete [] buffer;
|
|---|
| 580 | return _T("");
|
|---|
| 581 | }
|
|---|
| 582 | std::wstring ret = buffer;
|
|---|
| 583 | delete [] buffer;
|
|---|
| 584 | return ret;
|
|---|
| 585 | }
|
|---|
| 586 |
|
|---|
| 587 | /**
|
|---|
| 588 | * Wrapper function around the ModuleHelperInit call.
|
|---|
| 589 | * This wrapper retrieves all pointers and stores them for future use.
|
|---|
| 590 | * @param f A function pointer to a function that can be used to load function from the core.
|
|---|
| 591 | * @return NSCAPI::success or NSCAPI::failure
|
|---|
| 592 | */
|
|---|
| 593 | bool nscapi::core_wrapper::load_endpoints(unsigned int id, nscapi::core_api::lpNSAPILoader f) {
|
|---|
| 594 | id_ = id;
|
|---|
| 595 | fNSAPIGetApplicationName = (nscapi::core_api::lpNSAPIGetApplicationName)f(_T("NSAPIGetApplicationName"));
|
|---|
| 596 | fNSAPIGetApplicationVersionStr = (nscapi::core_api::lpNSAPIGetApplicationVersionStr)f(_T("NSAPIGetApplicationVersionStr"));
|
|---|
| 597 | fNSAPIGetSettingsInt = (nscapi::core_api::lpNSAPIGetSettingsInt)f(_T("NSAPIGetSettingsInt"));
|
|---|
| 598 | fNSAPIGetSettingsBool = (nscapi::core_api::lpNSAPIGetSettingsBool)f(_T("NSAPIGetSettingsBool"));
|
|---|
| 599 | fNSAPIGetSettingsString = (nscapi::core_api::lpNSAPIGetSettingsString)f(_T("NSAPIGetSettingsString"));
|
|---|
| 600 | fNSAPIGetSettingsSection = (nscapi::core_api::lpNSAPIGetSettingsSection)f(_T("NSAPIGetSettingsSection"));
|
|---|
| 601 | fNSAPIReleaseSettingsSectionBuffer = (nscapi::core_api::lpNSAPIReleaseSettingsSectionBuffer)f(_T("NSAPIReleaseSettingsSectionBuffer"));
|
|---|
| 602 | fNSAPIMessage = (nscapi::core_api::lpNSAPIMessage)f(_T("NSAPIMessage"));
|
|---|
| 603 | fNSAPIStopServer = (nscapi::core_api::lpNSAPIStopServer)f(_T("NSAPIStopServer"));
|
|---|
| 604 | //fNSAPIExit = (nscapi::core_api::lpNSAPIExit)f(_T("NSAPIExit"));
|
|---|
| 605 | fNSAPIInject = (nscapi::core_api::lpNSAPIInject)f(_T("NSAPIInject"));
|
|---|
| 606 | fNSAPIDestroyBuffer = (nscapi::core_api::lpNSAPIDestroyBuffer)f(_T("NSAPIDestroyBuffer"));
|
|---|
| 607 | fNSAPINotify = (nscapi::core_api::lpNSAPINotify)f(_T("NSAPINotify"));
|
|---|
| 608 | fNSAPIGetBasePath = (nscapi::core_api::lpNSAPIGetBasePath)f(_T("NSAPIGetBasePath"));
|
|---|
| 609 | fNSAPICheckLogMessages = (nscapi::core_api::lpNSAPICheckLogMessages)f(_T("NSAPICheckLogMessages"));
|
|---|
| 610 | fNSAPIDecrypt = (nscapi::core_api::lpNSAPIDecrypt)f(_T("NSAPIDecrypt"));
|
|---|
| 611 | fNSAPIEncrypt = (nscapi::core_api::lpNSAPIEncrypt)f(_T("NSAPIEncrypt"));
|
|---|
| 612 | fNSAPISetSettingsString = (nscapi::core_api::lpNSAPISetSettingsString)f(_T("NSAPISetSettingsString"));
|
|---|
| 613 | fNSAPISetSettingsInt = (nscapi::core_api::lpNSAPISetSettingsInt)f(_T("NSAPISetSettingsInt"));
|
|---|
| 614 | fNSAPIWriteSettings = (nscapi::core_api::lpNSAPIWriteSettings)f(_T("NSAPIWriteSettings"));
|
|---|
| 615 | fNSAPIReadSettings = (nscapi::core_api::lpNSAPIReadSettings)f(_T("NSAPIReadSettings"));
|
|---|
| 616 | fNSAPIRehash = (nscapi::core_api::lpNSAPIRehash)f(_T("NSAPIRehash"));
|
|---|
| 617 |
|
|---|
| 618 | fNSAPIDescribeCommand = (nscapi::core_api::lpNSAPIDescribeCommand)f(_T("NSAPIDescribeCommand"));
|
|---|
| 619 | fNSAPIGetAllCommandNames = (nscapi::core_api::lpNSAPIGetAllCommandNames)f(_T("NSAPIGetAllCommandNames"));
|
|---|
| 620 | fNSAPIReleaseAllCommandNamessBuffer = (nscapi::core_api::lpNSAPIReleaseAllCommandNamessBuffer)f(_T("NSAPIReleaseAllCommandNamessBuffer"));
|
|---|
| 621 | fNSAPIRegisterCommand = (nscapi::core_api::lpNSAPIRegisterCommand)f(_T("NSAPIRegisterCommand"));
|
|---|
| 622 |
|
|---|
| 623 | fNSAPISettingsRegKey = (nscapi::core_api::lpNSAPISettingsRegKey)f(_T("NSAPISettingsRegKey"));
|
|---|
| 624 | fNSAPISettingsRegPath = (nscapi::core_api::lpNSAPISettingsRegPath)f(_T("NSAPISettingsRegPath"));
|
|---|
| 625 |
|
|---|
| 626 | fNSAPIGetPluginList = (nscapi::core_api::lpNSAPIGetPluginList)f(_T("NSAPIGetPluginList"));
|
|---|
| 627 | fNSAPIReleasePluginList = (nscapi::core_api::lpNSAPIReleasePluginList)f(_T("NSAPIReleasePluginList"));
|
|---|
| 628 |
|
|---|
| 629 | fNSAPISettingsSave = (nscapi::core_api::lpNSAPISettingsSave)f(_T("NSAPISettingsSave"));
|
|---|
| 630 |
|
|---|
| 631 | fNSAPIExpandPath = (nscapi::core_api::lpNSAPIExpandPath)f(_T("NSAPIExpandPath"));
|
|---|
| 632 |
|
|---|
| 633 | return true;
|
|---|
| 634 | }
|
|---|