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