source: nscp/include/nscapi/nscapi_core_wrapper.cpp @ a87ce04

0.4.00.4.10.4.2
Last change on this file since a87ce04 was a87ce04, checked in by Michael Medin <michael@…>, 18 months ago
  • Fixed some issues in the NRPE decoder
  • Added support for forwarding queries (ie. xxx_forward) mainly usefull for python(?) scripts where we can now handconstruct messages for delivery
  • Added NRPE unit tests (52 of them)
  • Fixed process enumeration API for 64/32 bit
  • Property mode set to 100644
File size: 30.8 KB
Line 
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 <protobuf/plugin.pb.h>
34
35using namespace nscp::helpers;
36
37#define CORE_LOG_ERROR_STD(msg) CORE_LOG_ERROR(((std::wstring)msg).c_str())
38#define CORE_LOG_ERROR(msg) CORE_ANY_MSG(msg,NSCAPI::error)
39
40#define CORE_LOG_CRITICAL_STD(msg) CORE_LOG_CRITICAL(((std::wstring)msg).c_str())
41#define CORE_LOG_CRITICAL(msg) CORE_ANY_MSG(msg,NSCAPI::critical)
42
43#define CORE_LOG_MESSAGE_STD(msg) CORE_LOG_MESSAGE(((std::wstring)msg).c_str())
44#define CORE_LOG_MESSAGE(msg) CORE_ANY_MSG(msg,NSCAPI::log)
45
46#define CORE_DEBUG_MSG_STD(msg) CORE_DEBUG_MSG((std::wstring)msg)
47#define CORE_DEBUG_MSG(msg) CORE_ANY_MSG(msg,NSCAPI::debug)
48
49#define CORE_ANY_MSG(msg, type) log(type, __FILE__, __LINE__, msg)
50
51
52//////////////////////////////////////////////////////////////////////////
53// Callbacks into the core
54//////////////////////////////////////////////////////////////////////////
55
56/**
57 * Callback to send a message through to the core
58 *
59 * @param msgType Message type (debug, warning, etc.)
60 * @param file File where message was generated (__FILE__)
61 * @param line Line where message was generated (__LINE__)
62 * @param message Message in human readable format
63 * @throws nscapi::nscapi_exception When core pointer set is unavailable.
64 */
65void nscapi::core_wrapper::log(int msgType, std::string file, int line, std::wstring logMessage) {
66        if (fNSAPIMessage) {
67                if ((msgType == NSCAPI::debug) && (!logDebug()))
68                        return;
69                std::string str;
70                try {
71                        Plugin::LogEntry message;
72                        Plugin::LogEntry::Entry *msg = message.add_entry();
73                        msg->set_level(nscapi::functions::log_to_gpb(msgType));
74                        msg->set_file(file);
75                        msg->set_line(line);
76                        msg->set_message(utf8::cvt<std::string>(logMessage));
77                        if (!message.SerializeToString(&str)) {
78                                std::cout << "Failed to generate message";
79                        }
80                        return fNSAPIMessage(str.c_str(), str.size());
81                } catch (...) {
82                        std::wcout << _T("Failed to generate message: ");
83                }
84//              return fNSAPIMessage(to_string(logMessage).c_str(), logMessage.size());
85        }
86        else
87                std::wcout << _T("*** *** *** NSCore not loaded, dumping log: ") << to_wstring(file) << _T(":") << line << _T(": ") << std::endl << logMessage << std::endl;
88}
89void nscapi::core_wrapper::log(int msgType, std::string file, int line, std::string message) {
90        if ((msgType == NSCAPI::debug) && (!logDebug()))
91                return;
92        log(msgType, file, line, utf8::cvt<std::wstring>(message));
93}
94
95/**
96 * Inject a request command in the core (this will then be sent to the plug-in stack for processing)
97 * @param command Command to inject (password should not be included.
98 * @return The result (if any) of the command.
99 * @throws nscapi::nscapi_exception When core pointer set is unavailable or an unknown inject error occurs.
100 */
101
102/**
103 * Inject a request command in the core (this will then be sent to the plug-in stack for processing)
104 * @param command Command to inject
105 * @param argLen The length of the argument buffer
106 * @param **argument The argument buffer
107 * @param *returnMessageBuffer Buffer to hold the returned message
108 * @param returnMessageBufferLen Length of returnMessageBuffer
109 * @param *returnPerfBuffer Buffer to hold the returned performance data
110 * @param returnPerfBufferLen returnPerfBuffer
111 * @return The returned status of the command
112 */
113NSCAPI::nagiosReturn nscapi::core_wrapper::query(const wchar_t* command, const char *request, const unsigned int request_len, char **response, unsigned int *response_len)
114{
115        if (!fNSAPIInject)
116                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
117        return fNSAPIInject(command, request, request_len, response, response_len);
118}
119
120
121void nscapi::core_wrapper::DestroyBuffer(char**buffer) {
122        if (!fNSAPIDestroyBuffer)
123                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
124        return fNSAPIDestroyBuffer(buffer);
125}
126
127
128NSCAPI::errorReturn nscapi::core_wrapper::submit_message(std::wstring channel, std::string request, std::string &response) {
129
130        if (!fNSAPINotify)
131                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
132        char *buffer = NULL;
133        unsigned int buffer_size = 0;
134        NSCAPI::nagiosReturn ret = submit_message(channel.c_str(), request.c_str(), request.size(), &buffer, &buffer_size);
135
136        if (buffer_size > 0 && buffer != NULL) {
137                response = std::string(buffer, buffer_size);
138        }
139
140        DestroyBuffer(&buffer);
141        return ret;
142}
143
144NSCAPI::errorReturn nscapi::core_wrapper::reload(std::wstring module) {
145
146        if (!fNSAPIReload)
147                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
148        return fNSAPIReload(module.c_str());
149}
150
151bool nscapi::core_wrapper::submit_simple_message(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn code, std::wstring & message, std::wstring & perf, std::wstring & response) {
152        std::string request, buffer;
153        nscapi::functions::create_simple_submit_request(channel, command, code, message, perf, request);
154        NSCAPI::nagiosReturn ret = submit_message(channel, request, buffer);
155        if (ret == NSCAPI::returnIgnored) {
156                response = _T("No handler for this message");
157                return false;
158        }
159        if (buffer.size() == 0) {
160                response = _T("Missing response from submission");
161                return false;
162        }
163        nscapi::functions::parse_simple_submit_response(buffer, response);
164        return ret == NSCAPI::isSuccess;
165}
166
167NSCAPI::nagiosReturn nscapi::core_wrapper::submit_message(const wchar_t* channel, const char *request, const unsigned int request_len, char **response, unsigned int *response_len)
168{
169        if (!fNSAPINotify)
170                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
171        return fNSAPINotify(channel, request, request_len, response, response_len);
172}
173
174/**
175* Inject a request command in the core (this will then be sent to the plug-in stack for processing)
176* @param command Command to inject (password should not be included.
177* @param argLen The length of the argument buffer
178* @param **argument The argument buffer
179* @param message The return message buffer
180* @param perf The return performance data buffer
181* @return The return of the command
182*/
183NSCAPI::nagiosReturn nscapi::core_wrapper::simple_query(const std::wstring command, const std::list<std::wstring> & argument, std::wstring & msg, std::wstring & perf)
184{
185        if (!fNSAPIInject)
186                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
187        std::string response;
188        NSCAPI::nagiosReturn ret = simple_query(command, argument, response);
189        if (!response.empty()) {
190                try {
191                        return nscapi::functions::parse_simple_query_response(response, msg, perf);
192                } catch (std::exception &e) {
193                        CORE_LOG_ERROR_STD(_T("Failed to extract return message: ") + utf8::cvt<std::wstring>(e.what()));
194                        return NSCAPI::returnUNKNOWN;
195                }
196        }
197        return ret;
198}
199/**
200* Inject a request command in the core (this will then be sent to the plug-in stack for processing)
201* @param command Command to inject (password should not be included.
202* @param argLen The length of the argument buffer
203* @param **argument The argument buffer
204* @param message The return message buffer
205* @param perf The return performance data buffer
206* @return The return of the command
207*/
208NSCAPI::nagiosReturn nscapi::core_wrapper::simple_query(const std::wstring command, const std::list<std::wstring> & arguments, std::string & result)
209{
210        if (!fNSAPIInject)
211                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
212
213        std::string request;
214        try {
215                nscapi::functions::create_simple_query_request(command, arguments, request);
216        } catch (std::exception &e) {
217                CORE_LOG_ERROR_STD(_T("Failed to extract return message: ") + utf8::cvt<std::wstring>(e.what()));
218                return NSCAPI::returnUNKNOWN;
219        }
220        return query(command.c_str(), request, result);
221}
222
223NSCAPI::nagiosReturn nscapi::core_wrapper::query(const std::wstring & command, const std::string & request, std::string & result)
224{
225        if (!fNSAPIInject)
226                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
227        char *buffer = NULL;
228        unsigned int buffer_size = 0;
229        NSCAPI::nagiosReturn retC = query(command.c_str(), request.c_str(), request.size(), &buffer, &buffer_size);
230
231        if (buffer_size > 0 && buffer != NULL) {
232                //PluginCommand::ResponseMessage rsp_msg;
233                result = std::string(buffer, buffer_size);
234        }
235
236        DestroyBuffer(&buffer);
237        switch (retC) {
238                case NSCAPI::returnIgnored:
239                        CORE_LOG_MESSAGE_STD(_T("No handler for command '") + command + _T("'."));
240                        break;
241                case NSCAPI::returnOK:
242                case NSCAPI::returnCRIT:
243                case NSCAPI::returnWARN:
244                case NSCAPI::returnUNKNOWN:
245                        break;
246                default:
247                        throw nscapi::nscapi_exception(_T("Unknown return code when injecting: ") + std::wstring(command));
248        }
249        return retC;
250}
251
252
253NSCAPI::nagiosReturn nscapi::core_wrapper::simple_query_from_nrpe(const std::wstring command, const std::wstring & buffer, std::wstring & message, std::wstring & perf) {
254        if (!fNSAPIInject)
255                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
256        boost::tokenizer<boost::char_separator<wchar_t>, std::wstring::const_iterator, std::wstring > tok(buffer, boost::char_separator<wchar_t>(_T("!")));
257        std::list<std::wstring> arglist;
258        BOOST_FOREACH(std::wstring s, tok)
259                arglist.push_back(s);
260        return simple_query(command, arglist, message, perf);
261}
262
263NSCAPI::nagiosReturn nscapi::core_wrapper::exec_command(const std::wstring target, const std::wstring command, std::string request, std::string & result) {
264        char *buffer = NULL;
265        unsigned int buffer_size = 0;
266        NSCAPI::nagiosReturn retC = exec_command(target.c_str(), command.c_str(), request.c_str(), request.size(), &buffer, &buffer_size);
267
268        if (buffer_size > 0 && buffer != NULL) {
269                result = std::string(buffer, buffer_size);
270        }
271
272        DestroyBuffer(&buffer);
273        switch (retC) {
274                case NSCAPI::returnIgnored:
275                        CORE_LOG_MESSAGE_STD(_T("No handler for command '") + command + _T("'."));
276                        break;
277                case NSCAPI::returnOK:
278                case NSCAPI::returnCRIT:
279                case NSCAPI::returnWARN:
280                case NSCAPI::returnUNKNOWN:
281                        break;
282                default:
283                        throw nscapi::nscapi_exception(_T("Unknown return code when injecting: ") + std::wstring(command));
284        }
285        return retC;
286}
287NSCAPI::nagiosReturn nscapi::core_wrapper::exec_command(const wchar_t* target, const wchar_t* command, const char *request, const unsigned int request_len, char **response, unsigned int *response_len)
288{
289        if (!fNSAPIExecCommand)
290                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
291        return fNSAPIExecCommand(target, command, request, request_len, response, response_len);
292}
293
294NSCAPI::nagiosReturn nscapi::core_wrapper::exec_simple_command(const std::wstring target, const std::wstring command, const std::list<std::wstring> &argument, std::list<std::wstring> & result) {
295        std::string request, response;
296        nscapi::functions::create_simple_exec_request(command, argument, request);
297        NSCAPI::nagiosReturn ret = exec_command(target, command, request, response);
298        nscapi::functions::parse_simple_exec_result(response, result);
299        return ret;
300}
301
302
303
304/**
305 * Ask the core to shutdown (only works when run as a service, o/w does nothing ?
306 * @todo Check if this might cause damage if not run as a service.
307 */
308void nscapi::core_wrapper::StopService(void) {
309        if (fNSAPIStopServer)
310                fNSAPIStopServer();
311}
312/**
313 * Close the program (usefull for tray/testmode) without stopping the service (unless this is the service).
314 * @author mickem
315 */
316void nscapi::core_wrapper::Exit(void) {
317        if (fNSAPIExit)
318                fNSAPIExit();
319}
320/**
321 * Retrieve a string from the settings subsystem (INI-file)
322 * Might possibly be located in the registry in the future.
323 *
324 * @param section Section key (generally module specific, make sure this is "unique")
325 * @param key The key to retrieve
326 * @param defaultValue A default value (if no value is set in the settings file)
327 * @return the current value or defaultValue if no value is set.
328 * @throws nscapi::nscapi_exception When core pointer set is unavailable or an error occurs.
329 */
330std::wstring nscapi::core_wrapper::getSettingsString(std::wstring section, std::wstring key, std::wstring defaultValue) {
331        if (!fNSAPIGetSettingsString)
332                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
333        unsigned int buf_len = getBufferLength();
334        wchar_t *buffer = new wchar_t[buf_len+1];
335        if (fNSAPIGetSettingsString(section.c_str(), key.c_str(), defaultValue.c_str(), buffer, buf_len) != NSCAPI::isSuccess) {
336                delete [] buffer;
337                throw nscapi::nscapi_exception(_T("Settings could not be retrieved."));
338        }
339        std::wstring ret = buffer;
340        delete [] buffer;
341        return ret;
342}
343
344std::wstring nscapi::core_wrapper::expand_path(std::wstring value) {
345        if (!fNSAPIExpandPath)
346                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
347        unsigned int buf_len = getBufferLength();
348        wchar_t *buffer = new wchar_t[buf_len+1];
349        if (fNSAPIExpandPath(value.c_str(), buffer, buf_len) != NSCAPI::isSuccess) {
350                delete [] buffer;
351                throw nscapi::nscapi_exception(_T("Settings could not be retrieved."));
352        }
353        std::wstring ret = buffer;
354        delete [] buffer;
355        return ret;
356}
357/**
358 * Get a section of settings strings
359 * @param section The section to retrieve
360 * @return The keys in the section
361 */
362std::list<std::wstring> nscapi::core_wrapper::getSettingsSection(std::wstring section) {
363        if (!fNSAPIGetSettingsSection)
364                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
365        array_buffer::arrayBuffer aBuffer = NULL;
366        unsigned int argLen = 0;
367        if (fNSAPIGetSettingsSection(section.c_str(), &aBuffer, &argLen) != NSCAPI::isSuccess) {
368                throw nscapi::nscapi_exception(_T("Settings could not be retrieved."));
369        }
370        std::list<std::wstring> ret = array_buffer::arrayBuffer2list(argLen, aBuffer);
371        if (fNSAPIReleaseSettingsSectionBuffer(&aBuffer, &argLen) != NSCAPI::isSuccess) {
372                throw nscapi::nscapi_exception(_T("Settings could not be destroyed."));
373        }
374        if (aBuffer != NULL)
375                throw nscapi::nscapi_exception(_T("buffer is not null?."));
376        return ret;
377}
378std::list<std::wstring> nscapi::core_wrapper::getSettingsSections(std::wstring section) {
379        if (!fNSAPIGetSettingsSections)
380                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
381        array_buffer::arrayBuffer aBuffer = NULL;
382        unsigned int argLen = 0;
383        if (fNSAPIGetSettingsSections(section.c_str(), &aBuffer, &argLen) != NSCAPI::isSuccess) {
384                throw nscapi::nscapi_exception(_T("Settings could not be retrieved."));
385        }
386        std::list<std::wstring> ret = array_buffer::arrayBuffer2list(argLen, aBuffer);
387        if (fNSAPIReleaseSettingsSectionBuffer(&aBuffer, &argLen) != NSCAPI::isSuccess) {
388                throw nscapi::nscapi_exception(_T("Settings could not be destroyed."));
389        }
390        if (aBuffer != NULL)
391                throw nscapi::nscapi_exception(_T("buffer is not null?."));
392        return ret;
393}
394/**
395 * Retrieve an int from the settings subsystem (INI-file)
396 * Might possibly be located in the registry in the future.
397 *
398 * @param section Section key (generally module specific, make sure this is "unique")
399 * @param key The key to retrieve
400 * @param defaultValue A default value (if no value is set in the settings file)
401 * @return the current value or defaultValue if no value is set.
402 * @throws nscapi::nscapi_exception When core pointer set is unavailable.
403 */
404int nscapi::core_wrapper::getSettingsInt(std::wstring section, std::wstring key, int defaultValue) {
405        if (!fNSAPIGetSettingsInt)
406                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
407        return fNSAPIGetSettingsInt(section.c_str(), key.c_str(), defaultValue);
408}
409bool nscapi::core_wrapper::getSettingsBool(std::wstring section, std::wstring key, bool defaultValue) {
410        if (!fNSAPIGetSettingsBool)
411                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
412        return fNSAPIGetSettingsBool(section.c_str(), key.c_str(), defaultValue?1:0) == 1;
413}
414void 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) {
415        if (!fNSAPISettingsRegKey)
416                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
417        fNSAPISettingsRegKey(path.c_str(), key.c_str(), type, title.c_str(), description.c_str(), defaultValue.c_str(), advanced);
418}
419void nscapi::core_wrapper::settings_register_path(std::wstring path, std::wstring title, std::wstring description, bool advanced) {
420        if (!fNSAPISettingsRegPath)
421                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
422        fNSAPISettingsRegPath(path.c_str(), title.c_str(), description.c_str(), advanced);
423}
424
425
426void nscapi::core_wrapper::settings_save() {
427        if (!fNSAPISettingsSave)
428                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
429        fNSAPISettingsSave();
430}
431
432/**
433 * Retrieve the application name (in human readable format) from the core.
434 * @return A string representing the application name.
435 * @throws nscapi::nscapi_exception When core pointer set is unavailable or an unexpected error occurs.
436 */
437std::wstring nscapi::core_wrapper::getApplicationName() {
438        if (!fNSAPIGetApplicationName)
439                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
440        unsigned int buf_len = getBufferLength();
441        wchar_t *buffer = new wchar_t[buf_len+1];
442        if (fNSAPIGetApplicationName(buffer, buf_len) != NSCAPI::isSuccess) {
443                delete [] buffer;
444                throw nscapi::nscapi_exception(_T("Application name could not be retrieved"));
445        }
446        std::wstring ret = buffer;
447        delete [] buffer;
448        return ret;
449}
450/**
451 * Retrieve the directory root of the application from the core.
452 * @return A string representing the base path.
453 * @throws nscapi::nscapi_exception When core pointer set is unavailable or an unexpected error occurs.
454 */
455std::wstring nscapi::core_wrapper::getBasePath() {
456        if (!fNSAPIGetBasePath)
457                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
458        unsigned int buf_len = getBufferLength();
459        wchar_t *buffer = new wchar_t[buf_len+1];
460        if (fNSAPIGetBasePath(buffer, buf_len) != NSCAPI::isSuccess) {
461                delete [] buffer;
462                throw nscapi::nscapi_exception(_T("Base path could not be retrieved"));
463        }
464        std::wstring ret = buffer;
465        delete [] buffer;
466        return ret;
467}
468
469unsigned int nscapi::core_wrapper::getBufferLength() {
470        static unsigned int len = 0;
471        if (len == 0) {
472                len = getSettingsInt(setting_keys::settings_def::PAYLOAD_LEN_PATH, setting_keys::settings_def::PAYLOAD_LEN, setting_keys::settings_def::PAYLOAD_LEN_DEFAULT);
473        }
474        return len;
475}
476
477
478bool nscapi::core_wrapper::logDebug() {
479        enum status {unknown, debug, nodebug };
480        static status d = unknown;
481        if (d == unknown) {
482                if (checkLogMessages(debug)== NSCAPI::istrue)
483                        d = debug;
484                else
485                        d = nodebug;
486        }
487        return (d == debug);
488}
489
490std::wstring nscapi::core_wrapper::Encrypt(std::wstring str, unsigned int algorithm) {
491        if (!fNSAPIEncrypt)
492                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
493        unsigned int len = 0;
494        // @todo investigate potential problems with static_cast<unsigned int>
495        fNSAPIEncrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), NULL, &len);
496        len+=2;
497        wchar_t *buf = new wchar_t[len+1];
498        NSCAPI::errorReturn ret = fNSAPIEncrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), buf, &len);
499        if (ret == NSCAPI::isSuccess) {
500                std::wstring ret = buf;
501                delete [] buf;
502                return ret;
503        }
504        return _T("");
505}
506std::wstring nscapi::core_wrapper::Decrypt(std::wstring str, unsigned int algorithm) {
507        if (!fNSAPIDecrypt)
508                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
509        unsigned int len = 0;
510        // @todo investigate potential problems with: static_cast<unsigned int>(str.size())
511        fNSAPIDecrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), NULL, &len);
512        len+=2;
513        wchar_t *buf = new wchar_t[len+1];
514        NSCAPI::errorReturn ret = fNSAPIDecrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), buf, &len);
515        if (ret == NSCAPI::isSuccess) {
516                std::wstring ret = buf;
517                delete [] buf;
518                return ret;
519        }
520        return _T("");
521}
522NSCAPI::errorReturn nscapi::core_wrapper::SetSettingsString(std::wstring section, std::wstring key, std::wstring value) {
523        if (!fNSAPISetSettingsString)
524                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
525        return fNSAPISetSettingsString(section.c_str(), key.c_str(), value.c_str());
526}
527NSCAPI::errorReturn nscapi::core_wrapper::SetSettingsInt(std::wstring section, std::wstring key, int value) {
528        if (!fNSAPISetSettingsInt)
529                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
530        return fNSAPISetSettingsInt(section.c_str(), key.c_str(), value);
531}
532NSCAPI::errorReturn nscapi::core_wrapper::WriteSettings(int type) {
533        if (!fNSAPIWriteSettings)
534                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
535        return fNSAPIWriteSettings(type);
536}
537NSCAPI::errorReturn nscapi::core_wrapper::ReadSettings(int type) {
538        if (!fNSAPIReadSettings)
539                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
540        return fNSAPIReadSettings(type);
541}
542NSCAPI::errorReturn nscapi::core_wrapper::Rehash(int flag) {
543        if (!fNSAPIRehash)
544                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
545        return fNSAPIRehash(flag);
546}
547nscapi::core_wrapper::plugin_info_list nscapi::core_wrapper::getPluginList() {
548        if (!fNSAPIGetPluginList || !fNSAPIReleasePluginList)
549                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
550        plugin_info_list ret;
551       
552       
553        int len = 0;
554        //NSCAPI::plugin_info_list **list2;
555        //NSCAPI::plugin_info_list *list[1];
556        NSCAPI::plugin_info *list[1];
557        //typedef NSCAPI::errorReturn (*lpNSAPIGetPluginList)(int *len, NSAPI_plugin_info** list);
558        //typedef NSCAPI::errorReturn (*lpNSAPIReleasePluginList)(int len, NSAPI_plugin_info** list);
559        NSCAPI::errorReturn err = fNSAPIGetPluginList(&len, list);
560        if (err != NSCAPI::isSuccess)
561                return ret;
562        for (int i=0;i<len;i++) {
563                plugin_info_type info;
564                info.description = (*list)[i].description;
565                info.name = (*list)[i].name;
566                info.dll = (*list)[i].dll;
567                ret.push_back(info);
568        }
569        fNSAPIReleasePluginList(len, list);
570        return ret;
571}
572
573std::list<std::wstring> nscapi::core_wrapper::getAllCommandNames() {
574        if (!fNSAPIGetAllCommandNames || !fNSAPIReleaseAllCommandNamessBuffer )
575                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
576        array_buffer::arrayBuffer aBuffer = NULL;
577        unsigned int argLen = 0;
578        if (fNSAPIGetAllCommandNames(&aBuffer, &argLen) != NSCAPI::isSuccess) {
579                throw nscapi::nscapi_exception(_T("Commands could not be retrieved."));
580        }
581        std::list<std::wstring> ret = array_buffer::arrayBuffer2list(argLen, aBuffer);
582        if (fNSAPIReleaseAllCommandNamessBuffer(&aBuffer, &argLen) != NSCAPI::isSuccess) {
583                throw nscapi::nscapi_exception(_T("Commands could not be destroyed."));
584        }
585        if (aBuffer != NULL)
586                throw nscapi::nscapi_exception(_T("buffer is not null?."));
587        return ret;
588}
589std::wstring nscapi::core_wrapper::describeCommand(std::wstring command) {
590        if (!fNSAPIDescribeCommand)
591                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
592        unsigned int buf_len = getBufferLength();
593        wchar_t *buffer = new wchar_t[buf_len+1];
594        if (fNSAPIDescribeCommand(command.c_str(), buffer, buf_len) != NSCAPI::isSuccess) {
595                delete [] buffer;
596                throw nscapi::nscapi_exception(_T("Base path could not be retrieved"));
597        }
598        std::wstring ret = buffer;
599        delete [] buffer;
600        return ret;
601}
602void nscapi::core_wrapper::registerCommand(unsigned int id, std::wstring command, std::wstring description) {
603        if (!fNSAPIRegisterCommand)
604                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
605        if (fNSAPIRegisterCommand(id, command.c_str(), description.c_str()) != NSCAPI::isSuccess) {
606                CORE_LOG_ERROR_STD(_T("Failed to register command: ") + command + _T(" in plugin: ") + to_wstring(id));
607        }
608}
609
610void nscapi::core_wrapper::registerSubmissionListener(unsigned int id, std::wstring channel) {
611        if (!fNSAPIRegisterSubmissionListener)
612                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
613        if (fNSAPIRegisterSubmissionListener(id, channel.c_str()) != NSCAPI::isSuccess) {
614                CORE_LOG_ERROR_STD(_T("Failed to register channel: ") + channel + _T(" in plugin: ") + to_wstring(id));
615        }
616}
617void nscapi::core_wrapper::registerRoutingListener(unsigned int id, std::wstring channel) {
618        if (!fNSAPIRegisterRoutingListener)
619                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
620        if (fNSAPIRegisterRoutingListener(id, channel.c_str()) != NSCAPI::isSuccess) {
621                CORE_LOG_ERROR_STD(_T("Failed to register channel: ") + channel + _T(" in plugin: ") + to_wstring(id));
622        }
623}
624
625
626bool nscapi::core_wrapper::checkLogMessages(int type) {
627        if (!fNSAPICheckLogMessages)
628                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
629        return fNSAPICheckLogMessages(type) == NSCAPI::istrue;
630}
631/**
632 * Retrieve the application version as a string (in human readable format) from the core.
633 * @return A string representing the application version.
634 * @throws nscapi::nscapi_exception When core pointer set is unavailable.
635 */
636std::wstring nscapi::core_wrapper::getApplicationVersionString() {
637        if (!fNSAPIGetApplicationVersionStr)
638                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
639        unsigned int buf_len = getBufferLength();
640        wchar_t *buffer = new wchar_t[buf_len+1];
641        if (fNSAPIGetApplicationVersionStr(buffer, buf_len) != NSCAPI::isSuccess) {
642                delete [] buffer;
643                return _T("");
644        }
645        std::wstring ret = buffer;
646        delete [] buffer;
647        return ret;
648}
649
650/**
651 * Wrapper function around the ModuleHelperInit call.
652 * This wrapper retrieves all pointers and stores them for future use.
653 * @param f A function pointer to a function that can be used to load function from the core.
654 * @return NSCAPI::success or NSCAPI::failure
655 */
656bool nscapi::core_wrapper::load_endpoints(nscapi::core_api::lpNSAPILoader f) {
657        fNSAPIGetApplicationName = (nscapi::core_api::lpNSAPIGetApplicationName)f(_T("NSAPIGetApplicationName"));
658        fNSAPIGetApplicationVersionStr = (nscapi::core_api::lpNSAPIGetApplicationVersionStr)f(_T("NSAPIGetApplicationVersionStr"));
659        fNSAPIGetSettingsInt = (nscapi::core_api::lpNSAPIGetSettingsInt)f(_T("NSAPIGetSettingsInt"));
660        fNSAPIGetSettingsBool = (nscapi::core_api::lpNSAPIGetSettingsBool)f(_T("NSAPIGetSettingsBool"));
661        fNSAPIGetSettingsString = (nscapi::core_api::lpNSAPIGetSettingsString)f(_T("NSAPIGetSettingsString"));
662        fNSAPIGetSettingsSection = (nscapi::core_api::lpNSAPIGetSettingsSection)f(_T("NSAPIGetSettingsSection"));
663        fNSAPIGetSettingsSections = (nscapi::core_api::lpNSAPIGetSettingsSections)f(_T("NSAPIGetSettingsSections"));
664        fNSAPIReleaseSettingsSectionBuffer = (nscapi::core_api::lpNSAPIReleaseSettingsSectionBuffer)f(_T("NSAPIReleaseSettingsSectionBuffer"));
665        fNSAPIMessage = (nscapi::core_api::lpNSAPIMessage)f(_T("NSAPIMessage"));
666        fNSAPIStopServer = (nscapi::core_api::lpNSAPIStopServer)f(_T("NSAPIStopServer"));
667        //fNSAPIExit = (nscapi::core_api::lpNSAPIExit)f(_T("NSAPIExit"));
668        fNSAPIInject = (nscapi::core_api::lpNSAPIInject)f(_T("NSAPIInject"));
669        fNSAPIExecCommand = (nscapi::core_api::lpNSAPIExecCommand)f(_T("NSAPIExecCommand"));
670        fNSAPIDestroyBuffer = (nscapi::core_api::lpNSAPIDestroyBuffer)f(_T("NSAPIDestroyBuffer"));
671        fNSAPINotify = (nscapi::core_api::lpNSAPINotify)f(_T("NSAPINotify"));
672        fNSAPIGetBasePath = (nscapi::core_api::lpNSAPIGetBasePath)f(_T("NSAPIGetBasePath"));
673        fNSAPICheckLogMessages = (nscapi::core_api::lpNSAPICheckLogMessages)f(_T("NSAPICheckLogMessages"));
674        fNSAPIDecrypt = (nscapi::core_api::lpNSAPIDecrypt)f(_T("NSAPIDecrypt"));
675        fNSAPIEncrypt = (nscapi::core_api::lpNSAPIEncrypt)f(_T("NSAPIEncrypt"));
676        fNSAPISetSettingsString = (nscapi::core_api::lpNSAPISetSettingsString)f(_T("NSAPISetSettingsString"));
677        fNSAPISetSettingsInt = (nscapi::core_api::lpNSAPISetSettingsInt)f(_T("NSAPISetSettingsInt"));
678        fNSAPIWriteSettings = (nscapi::core_api::lpNSAPIWriteSettings)f(_T("NSAPIWriteSettings"));
679        fNSAPIReadSettings = (nscapi::core_api::lpNSAPIReadSettings)f(_T("NSAPIReadSettings"));
680        fNSAPIRehash = (nscapi::core_api::lpNSAPIRehash)f(_T("NSAPIRehash"));
681        fNSAPIReload = (nscapi::core_api::lpNSAPIReload)f(_T("NSAPIReload"));
682
683        fNSAPIDescribeCommand = (nscapi::core_api::lpNSAPIDescribeCommand)f(_T("NSAPIDescribeCommand"));
684        fNSAPIGetAllCommandNames = (nscapi::core_api::lpNSAPIGetAllCommandNames)f(_T("NSAPIGetAllCommandNames"));
685        fNSAPIReleaseAllCommandNamessBuffer = (nscapi::core_api::lpNSAPIReleaseAllCommandNamessBuffer)f(_T("NSAPIReleaseAllCommandNamessBuffer"));
686        fNSAPIRegisterCommand = (nscapi::core_api::lpNSAPIRegisterCommand)f(_T("NSAPIRegisterCommand"));
687
688        fNSAPISettingsRegKey = (nscapi::core_api::lpNSAPISettingsRegKey)f(_T("NSAPISettingsRegKey"));
689        fNSAPISettingsRegPath = (nscapi::core_api::lpNSAPISettingsRegPath)f(_T("NSAPISettingsRegPath"));
690
691        fNSAPIGetPluginList = (nscapi::core_api::lpNSAPIGetPluginList)f(_T("NSAPIGetPluginList"));
692        fNSAPIReleasePluginList = (nscapi::core_api::lpNSAPIReleasePluginList)f(_T("NSAPIReleasePluginList"));
693
694        fNSAPISettingsSave = (nscapi::core_api::lpNSAPISettingsSave)f(_T("NSAPISettingsSave"));
695
696        fNSAPIExpandPath = (nscapi::core_api::lpNSAPIExpandPath)f(_T("NSAPIExpandPath"));
697       
698        fNSAPIRegisterSubmissionListener = (nscapi::core_api::lpNSAPIRegisterSubmissionListener)f(_T("NSAPIRegisterSubmissionListener"));
699        fNSAPIRegisterRoutingListener = (nscapi::core_api::lpNSAPIRegisterRoutingListener)f(_T("NSAPIRegisterRoutingListener"));
700
701        return true;
702}
Note: See TracBrowser for help on using the repository browser.