source: nscp/include/nscapi/nscapi_core_wrapper.cpp

Last change on this file was f542694, checked in by Michael Medin <michael@…>, 4 weeks ago

Yet another massive update with mainly internal stuff
The most interesting thing is I guess the new documentation toolkit which is now "working" (not much content yet, but it will come)

  • Property mode set to 100644
File size: 15.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 <iostream>
23
24#include <nscapi/nscapi_core_wrapper.hpp>
25#include <nscapi/nscapi_helper.hpp>
26
27#include <strEx.h>
28
29#define CORE_LOG_ERROR_STD(msg) if (should_log(NSCAPI::log_level::error)) { log(NSCAPI::log_level::error, __FILE__, __LINE__, (std::string)msg); }
30#define CORE_LOG_ERROR(msg) if (should_log(NSCAPI::log_level::error)) { log(NSCAPI::log_level::error, __FILE__, __LINE__, msg); }
31#define CORE_LOG_ERROR_WA(msg, wa) if (should_log(NSCAPI::log_level::error)) { log(NSCAPI::log_level::error, __FILE__, __LINE__, msg + utf8::cvt<std::string>(wa)); }
32
33#define LEGACY_BUFFER_LENGTH 4096
34//////////////////////////////////////////////////////////////////////////
35// Callbacks into the core
36//////////////////////////////////////////////////////////////////////////
37
38bool nscapi::core_wrapper::should_log(NSCAPI::nagiosReturn msgType) {
39        enum log_status {unknown, set };
40        static NSCAPI::log_level::level level = NSCAPI::log_level::info;
41        static log_status status = unknown;
42        if (status == unknown) {
43                level = get_loglevel();
44                status = set;
45        }
46        return nscapi::logging::matches(level, msgType);
47}
48
49/**
50* Callback to send a message through to the core
51*
52* @param msgType Message type (debug, warning, etc.)
53* @param file File where message was generated (__FILE__)
54* @param line Line where message was generated (__LINE__)
55* @param message Message in human readable format
56* @throws nscapi::nscapi_exception When core pointer set is unavailable.
57*/
58void nscapi::core_wrapper::log(NSCAPI::nagiosReturn msgType, std::string file, int line, std::string logMessage) {
59        if (!should_log(msgType))
60                return;
61        if (!fNSAPISimpleMessage) {
62                return;
63        }
64        try {
65                return fNSAPISimpleMessage(utf8::cvt<std::string>(alias).c_str(), msgType, file.c_str(), line, logMessage.c_str());
66        } catch (...) {
67        }
68}
69
70NSCAPI::log_level::level nscapi::core_wrapper::get_loglevel() {
71        if (!fNSAPIGetLoglevel) {
72                return NSCAPI::log_level::debug;
73        }
74        return fNSAPIGetLoglevel();
75}
76
77/**
78* Inject a request command in the core (this will then be sent to the plug-in stack for processing)
79* @param command Command to inject (password should not be included.
80* @return The result (if any) of the command.
81* @throws nscapi::nscapi_exception When core pointer set is unavailable or an unknown inject error occurs.
82*/
83
84/**
85* Inject a request command in the core (this will then be sent to the plug-in stack for processing)
86* @param command Command to inject
87* @param argLen The length of the argument buffer
88* @param **argument The argument buffer
89* @param *returnMessageBuffer Buffer to hold the returned message
90* @param returnMessageBufferLen Length of returnMessageBuffer
91* @param *returnPerfBuffer Buffer to hold the returned performance data
92* @param returnPerfBufferLen returnPerfBuffer
93* @return The returned status of the command
94*/
95NSCAPI::nagiosReturn nscapi::core_wrapper::query(const char *request, const unsigned int request_len, char **response, unsigned int *response_len)
96{
97        if (!fNSAPIInject)
98                throw nscapi::nscapi_exception("NSCore has not been initiated...");
99        return fNSAPIInject(request, request_len, response, response_len);
100}
101
102void nscapi::core_wrapper::DestroyBuffer(char**buffer) {
103        if (!fNSAPIDestroyBuffer)
104                throw nscapi::nscapi_exception("NSCore has not been initiated...");
105        return fNSAPIDestroyBuffer(buffer);
106}
107
108NSCAPI::errorReturn nscapi::core_wrapper::submit_message(std::string channel, std::string request, std::string &response) {
109        if (!fNSAPINotify)
110                throw nscapi::nscapi_exception("NSCore has not been initiated...");
111        char *buffer = NULL;
112        unsigned int buffer_size = 0;
113        NSCAPI::nagiosReturn ret = submit_message(channel.c_str(), request.c_str(), static_cast<unsigned int>(request.size()), &buffer, &buffer_size);
114
115        if (buffer_size > 0 && buffer != NULL) {
116                response = std::string(buffer, buffer_size);
117        }
118
119        DestroyBuffer(&buffer);
120        return ret;
121}
122
123NSCAPI::errorReturn nscapi::core_wrapper::reload(std::string module) {
124        if (!fNSAPIReload)
125                throw nscapi::nscapi_exception("NSCore has not been initiated...");
126        return fNSAPIReload(module.c_str());
127}
128NSCAPI::nagiosReturn nscapi::core_wrapper::submit_message(const char* channel, const char *request, const unsigned int request_len, char **response, unsigned int *response_len)
129{
130        if (!fNSAPINotify)
131                throw nscapi::nscapi_exception("NSCore has not been initiated...");
132        return fNSAPINotify(channel, request, request_len, response, response_len);
133}
134
135NSCAPI::nagiosReturn nscapi::core_wrapper::query(const std::string & request, std::string & result)
136{
137        if (!fNSAPIInject)
138                throw nscapi::nscapi_exception("NSCore has not been initiated...");
139        char *buffer = NULL;
140        unsigned int buffer_size = 0;
141        NSCAPI::nagiosReturn retC = query(request.c_str(), request.size(), &buffer, &buffer_size);
142
143        if (buffer_size > 0 && buffer != NULL) {
144                //PluginCommand::ResponseMessage rsp_msg;
145                result = std::string(buffer, buffer_size);
146        }
147
148        DestroyBuffer(&buffer);
149        if (retC != NSCAPI::isSuccess) {
150                CORE_LOG_ERROR("Failed to execute command");
151        }
152        return retC;
153}
154
155NSCAPI::nagiosReturn nscapi::core_wrapper::exec_command(const std::string target, std::string request, std::string & result) {
156        char *buffer = NULL;
157        unsigned int buffer_size = 0;
158        NSCAPI::nagiosReturn retC = exec_command(target.c_str(), request.c_str(), request.size(), &buffer, &buffer_size);
159
160        if (buffer_size > 0 && buffer != NULL) {
161                result = std::string(buffer, buffer_size);
162        }
163
164        DestroyBuffer(&buffer);
165        if (retC != NSCAPI::isSuccess) {
166                CORE_LOG_ERROR("Failed to execute command");
167        }
168        return retC;
169}
170NSCAPI::nagiosReturn nscapi::core_wrapper::exec_command(const char* target, const char *request, const unsigned int request_len, char **response, unsigned int *response_len)
171{
172        if (!fNSAPIExecCommand)
173                throw nscapi::nscapi_exception("NSCore has not been initiated...");
174        return fNSAPIExecCommand(target, request, request_len, response, response_len);
175}
176
177std::string nscapi::core_wrapper::expand_path(std::string value) {
178        if (!fNSAPIExpandPath)
179                throw nscapi::nscapi_exception("NSCore has not been initiated...");
180        unsigned int buf_len = LEGACY_BUFFER_LENGTH;
181        char *buffer = new char[buf_len+1];
182        if (fNSAPIExpandPath(value.c_str(), buffer, buf_len) != NSCAPI::isSuccess) {
183                delete [] buffer;
184                throw nscapi::nscapi_exception("Settings could not be retrieved.");
185        }
186        std::string ret = buffer;
187        delete [] buffer;
188        return ret;
189}
190NSCAPI::errorReturn nscapi::core_wrapper::settings_query(const char *request, const unsigned int request_len, char **response, unsigned int *response_len) {
191        if (!fNSAPISettingsQuery)
192                throw nscapi::nscapi_exception("NSCore has not been initiated...");
193        return fNSAPISettingsQuery(request, request_len, response, response_len);
194}
195bool nscapi::core_wrapper::settings_query(const std::string request, std::string &response) {
196        char *buffer = NULL;
197        unsigned int buffer_size = 0;
198        NSCAPI::errorReturn retC = settings_query(request.c_str(), request.size(), &buffer, &buffer_size);
199        if (buffer_size > 0 && buffer != NULL) {
200                response = std::string(buffer, buffer_size);
201        }
202        DestroyBuffer(&buffer);
203        return retC == NSCAPI::isSuccess;
204}
205
206NSCAPI::errorReturn nscapi::core_wrapper::registry_query(const char *request, const unsigned int request_len, char **response, unsigned int *response_len) {
207        if (!fNSAPIRegistryQuery)
208                throw nscapi::nscapi_exception("NSCore has not been initiated...");
209        return fNSAPIRegistryQuery(request, request_len, response, response_len);
210}
211NSCAPI::errorReturn nscapi::core_wrapper::registry_query(const std::string request, std::string &response) {
212        char *buffer = NULL;
213        unsigned int buffer_size = 0;
214        NSCAPI::errorReturn retC = registry_query(request.c_str(), request.size(), &buffer, &buffer_size);
215        if (buffer_size > 0 && buffer != NULL) {
216                response = std::string(buffer, buffer_size);
217        }
218        DestroyBuffer(&buffer);
219        return retC;
220}
221
222bool nscapi::core_wrapper::json_to_protobuf(const std::string &request, std::string &response) {
223        char *buffer = NULL;
224        unsigned int buffer_size = 0;
225        NSCAPI::errorReturn retC = json_to_protobuf(request.c_str(), request.size(), &buffer, &buffer_size);
226        if (buffer_size > 0 && buffer != NULL) {
227                response = std::string(buffer, buffer_size);
228        }
229        DestroyBuffer(&buffer);
230        return retC == NSCAPI::isSuccess;
231}
232
233NSCAPI::errorReturn nscapi::core_wrapper::protobuf_to_json(const char *object, const char *request, const unsigned int request_len, char **response, unsigned int *response_len) {
234        if (!fNSCAPIProtobuf2Json)
235                throw nscapi::nscapi_exception("NSCore has not been initiated...");
236        return fNSCAPIProtobuf2Json(object, request, request_len, response, response_len);
237}
238
239bool nscapi::core_wrapper::protobuf_to_json(const std::string &object, const std::string &request, std::string &response) {
240        char *buffer = NULL;
241        unsigned int buffer_size = 0;
242        NSCAPI::errorReturn retC = protobuf_to_json(object.c_str(), request.c_str(), request.size(), &buffer, &buffer_size);
243        if (buffer_size > 0 && buffer != NULL) {
244                response = std::string(buffer, buffer_size);
245        }
246        DestroyBuffer(&buffer);
247        return retC == NSCAPI::isSuccess;
248}
249
250NSCAPI::errorReturn nscapi::core_wrapper::json_to_protobuf(const char *request, const unsigned int request_len, char **response, unsigned int *response_len) {
251        if (!fNSCAPIJson2Protobuf)
252                throw nscapi::nscapi_exception("NSCore has not been initiated...");
253        return fNSCAPIJson2Protobuf(request, request_len, response, response_len);
254}
255
256/**
257* Retrieve the application name (in human readable format) from the core.
258* @return A string representing the application name.
259* @throws nscapi::nscapi_exception When core pointer set is unavailable or an unexpected error occurs.
260*/
261std::string nscapi::core_wrapper::getApplicationName() {
262        if (!fNSAPIGetApplicationName)
263                throw nscapi::nscapi_exception("NSCore has not been initiated...");
264        unsigned int buf_len = LEGACY_BUFFER_LENGTH;
265        char *buffer = new char[buf_len+1];
266        if (fNSAPIGetApplicationName(buffer, buf_len) != NSCAPI::isSuccess) {
267                delete [] buffer;
268                throw nscapi::nscapi_exception("Application name could not be retrieved");
269        }
270        std::string ret = buffer;
271        delete [] buffer;
272        return ret;
273}
274
275std::wstring nscapi::core_wrapper::Encrypt(std::wstring str, unsigned int algorithm) {
276        if (!fNSAPIEncrypt)
277                throw nscapi::nscapi_exception("NSCore has not been initiated...");
278        unsigned int len = 0;
279        // @todo investigate potential problems with static_cast<unsigned int>
280        fNSAPIEncrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), NULL, &len);
281        len+=2;
282        wchar_t *buf = new wchar_t[len+1];
283        NSCAPI::errorReturn ret = fNSAPIEncrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), buf, &len);
284        if (ret == NSCAPI::isSuccess) {
285                std::wstring ret = buf;
286                delete [] buf;
287                return ret;
288        }
289        return _T("");
290}
291std::wstring nscapi::core_wrapper::Decrypt(std::wstring str, unsigned int algorithm) {
292        if (!fNSAPIDecrypt)
293                throw nscapi::nscapi_exception("NSCore has not been initiated...");
294        unsigned int len = 0;
295        // @todo investigate potential problems with: static_cast<unsigned int>(str.size())
296        fNSAPIDecrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), NULL, &len);
297        len+=2;
298        wchar_t *buf = new wchar_t[len+1];
299        NSCAPI::errorReturn ret = fNSAPIDecrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), buf, &len);
300        if (ret == NSCAPI::isSuccess) {
301                std::wstring ret = buf;
302                delete [] buf;
303                return ret;
304        }
305        return _T("");
306}
307
308bool nscapi::core_wrapper::checkLogMessages(int type) {
309        if (!fNSAPICheckLogMessages)
310                throw nscapi::nscapi_exception("NSCore has not been initiated...");
311        return fNSAPICheckLogMessages(type) == NSCAPI::istrue;
312}
313/**
314* Retrieve the application version as a string (in human readable format) from the core.
315* @return A string representing the application version.
316* @throws nscapi::nscapi_exception When core pointer set is unavailable.
317*/
318std::string nscapi::core_wrapper::getApplicationVersionString() {
319        if (!fNSAPIGetApplicationVersionStr)
320                throw nscapi::nscapi_exception("NSCore has not been initiated...");
321        unsigned int buf_len = LEGACY_BUFFER_LENGTH;
322        char *buffer = new char[buf_len+1];
323        if (fNSAPIGetApplicationVersionStr(buffer, buf_len) != NSCAPI::isSuccess) {
324                delete [] buffer;
325                return "";
326        }
327        std::string ret = buffer;
328        delete [] buffer;
329        return ret;
330}
331
332void nscapi::core_wrapper::set_alias(const std::string default_alias_, const std::string alias_) {
333        alias = default_alias_;
334}
335
336/**
337* Wrapper function around the ModuleHelperInit call.
338* This wrapper retrieves all pointers and stores them for future use.
339* @param f A function pointer to a function that can be used to load function from the core.
340* @return NSCAPI::success or NSCAPI::failure
341*/
342bool nscapi::core_wrapper::load_endpoints(nscapi::core_api::lpNSAPILoader f) {
343        fNSAPIGetApplicationName = (nscapi::core_api::lpNSAPIGetApplicationName)f("NSAPIGetApplicationName");
344        fNSAPIGetApplicationVersionStr = (nscapi::core_api::lpNSAPIGetApplicationVersionStr)f("NSAPIGetApplicationVersionStr");
345        fNSAPIMessage = (nscapi::core_api::lpNSAPIMessage)f("NSAPIMessage");
346        fNSAPISimpleMessage = (nscapi::core_api::lpNSAPISimpleMessage)f("NSAPISimpleMessage");
347        fNSAPIInject = (nscapi::core_api::lpNSAPIInject)f("NSAPIInject");
348        fNSAPIExecCommand = (nscapi::core_api::lpNSAPIExecCommand)f("NSAPIExecCommand");
349        fNSAPIDestroyBuffer = (nscapi::core_api::lpNSAPIDestroyBuffer)f("NSAPIDestroyBuffer");
350        fNSAPINotify = (nscapi::core_api::lpNSAPINotify)f("NSAPINotify");
351        fNSAPICheckLogMessages = (nscapi::core_api::lpNSAPICheckLogMessages)f("NSAPICheckLogMessages");
352        fNSAPIDecrypt = (nscapi::core_api::lpNSAPIDecrypt)f("NSAPIDecrypt");
353        fNSAPIEncrypt = (nscapi::core_api::lpNSAPIEncrypt)f("NSAPIEncrypt");
354        fNSAPIReload = (nscapi::core_api::lpNSAPIReload)f("NSAPIReload");
355
356        fNSAPISettingsQuery = (nscapi::core_api::lpNSAPISettingsQuery)f("NSAPISettingsQuery");
357        fNSAPIRegistryQuery = (nscapi::core_api::lpNSAPIRegistryQuery)f("NSAPIRegistryQuery");
358        fNSAPIExpandPath = (nscapi::core_api::lpNSAPIExpandPath)f("NSAPIExpandPath");
359
360        fNSAPIGetLoglevel = (nscapi::core_api::lpNSAPIGetLoglevel)f("NSAPIGetLoglevel");
361
362        fNSCAPIJson2Protobuf = (nscapi::core_api::lpNSCAPIJson2Protobuf)f("NSCAPIJson2Protobuf");
363        fNSCAPIProtobuf2Json = (nscapi::core_api::lpNSCAPIProtobuf2Json)f("NSCAPIProtobuf2Json");
364
365        return true;
366}
Note: See TracBrowser for help on using the repository browser.