source: nscp/include/nscapi/nscapi_plugin_wrapper.hpp @ 234a037

0.4.00.4.10.4.2
Last change on this file since 234a037 was 9b9be81, checked in by Michael Medin <michael@…>, 20 months ago
  • Fixed many many issues all over the place as I tried to make things working for my pressention at nwc.
  • Property mode set to 100644
File size: 14.0 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#pragma once
22
23#include <string>
24#include <list>
25#include <vector>
26#include <map>
27
28#include <boost/make_shared.hpp>
29#include <unicode_char.hpp>
30
31#include <NSCAPI.h>
32#include <nscapi/settings_proxy.hpp>
33#include <nscapi/macros.hpp>
34#include <nscapi/nscapi_helper.hpp>
35
36
37namespace PluginCommand {
38        class Request;
39        class Response;
40
41};
42namespace nscapi {
43        class plugin_wrapper {
44        public:
45                struct module_version {
46                        int major;
47                        int minor;
48                        int revision;
49                };
50
51                plugin_wrapper() : hModule_(NULL) {}
52                int wrapReturnString(wchar_t *buffer, unsigned int bufLen, std::wstring str, int defaultReturnCode);
53
54#ifdef WIN32
55        private:
56                HINSTANCE hModule_;
57        public:
58                int wrapDllMain(HANDLE hModule, DWORD ul_reason_for_call);
59                HINSTANCE getModule() {
60                        hModule_;
61                }
62#else
63                void* hModule_;
64#endif
65                int wrapModuleHelperInit(unsigned int id, nscapi::core_api::lpNSAPILoader f);;
66                NSCAPI::errorReturn wrapGetModuleName(wchar_t* buf, unsigned int buflen, std::wstring str);
67                int wrapLoadModule(bool success);
68                NSCAPI::errorReturn wrapGetModuleVersion(int *major, int *minor, int *revision, module_version version);
69                NSCAPI::boolReturn wrapHasCommandHandler(bool has);
70                NSCAPI::boolReturn wrapHasMessageHandler(bool has);
71                NSCAPI::boolReturn wrapHasRoutingHandler(bool has);
72                NSCAPI::boolReturn wrapHasNotificationHandler(bool has);
73                NSCAPI::nagiosReturn wrapHandleNotification(NSCAPI::nagiosReturn retResult);
74                int wrapUnloadModule(bool success);
75                NSCAPI::nagiosReturn wrapHandleCommand(NSCAPI::nagiosReturn retResult, const std::string &reply, char **reply_buffer, unsigned int *size);
76                NSCAPI::nagiosReturn wrapCommandLineExec(NSCAPI::nagiosReturn retResult, const std::string &reply, char **reply_buffer, unsigned int *size);
77                NSCAPI::nagiosReturn wrapRouteMessage(NSCAPI::nagiosReturn retResult);
78               
79                void wrapDeleteBuffer(char**buffer);
80
81        };
82
83        class core_wrapper;
84        class helper_singleton {
85                core_wrapper* core_;
86                plugin_wrapper *plugin_;
87        public:
88                helper_singleton();
89                core_wrapper* get_core() {
90                        return core_;
91                }
92                plugin_wrapper* get_plugin() {
93                        return plugin_;
94                }
95        };
96
97        extern helper_singleton* plugin_singleton;
98
99
100
101        namespace impl {
102                struct simple_plugin {
103                        int id_;
104                        inline nscapi::core_wrapper* get_core() {
105                                return plugin_singleton->get_core();
106                        }
107                        inline unsigned int get_id() {
108                                return id_;
109                        }
110                        inline void set_id(unsigned int id) {
111                                id_ = id;
112                        }
113                        inline boost::shared_ptr<nscapi::settings_proxy> get_settings_proxy() {
114                                return boost::shared_ptr<nscapi::settings_proxy>(new nscapi::settings_proxy(get_core()));
115                        }
116                        void register_command(std::wstring command, std::wstring description) {
117                                get_core()->registerCommand(get_id(), command, description);
118                        }
119
120                };
121
122                class simple_submission_handler {
123                public:
124                        NSCAPI::nagiosReturn handleRAWNotification(const wchar_t* channel, std::string request, std::string &response);
125                        virtual NSCAPI::nagiosReturn handleSimpleNotification(const std::wstring channel, const std::wstring source, const std::wstring command, NSCAPI::nagiosReturn code, std::wstring msg, std::wstring perf) = 0;
126
127                };
128
129                class simple_command_handler {
130                public:
131                        NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response);
132                        virtual NSCAPI::nagiosReturn handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &msg, std::wstring &perf) = 0;
133                };
134                class simple_command_line_exec {
135                public:
136                        NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response);
137                        virtual NSCAPI::nagiosReturn commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) = 0;
138                };
139
140
141                class simple_log_handler {
142                public:
143                        void handleMessageRAW(std::string data);
144                        virtual void handleMessage(int msgType, const std::string file, int line, std::string message) = 0;
145                };
146
147        };
148
149
150
151        template<class impl_type>
152        struct plugin_instance_data {
153                typedef std::map<unsigned int, boost::shared_ptr<impl_type> > plugin_list_type;
154                plugin_list_type plugins;
155                boost::shared_ptr<impl_type> get(unsigned int id) {
156                        typename plugin_list_type::iterator it = plugins.find(id);
157                        if (it != plugins.end())
158                                return it->second;
159                        boost::shared_ptr<impl_type> impl = boost::shared_ptr<impl_type>(new impl_type());
160                        plugins[id] = impl;
161                        return impl;
162                }
163                void erase(unsigned int id) {
164                        plugins.erase(id);
165                }
166        };
167
168        struct helpers {
169                static void wrap_string(std::string &string, char** buffer, unsigned int *buffer_len) {
170                        // TODO: Make this global to allow remote deletion!!!
171                        unsigned int buf_len = string.size();
172                        *buffer = new char[buf_len + 10];
173                        memcpy(*buffer, string.c_str(), buf_len+1);
174                        (*buffer)[buf_len] = 0;
175                        (*buffer)[buf_len+1] = 0;
176                        *buffer_len = buf_len;
177                }
178                int static wrap_string(wchar_t *buffer, unsigned int bufLen, std::wstring str, int defaultReturnCode ) {
179                        // @todo deprecate this
180                        if (str.length() >= bufLen) {
181                                std::wstring sstr = str.substr(0, bufLen-2);
182                                NSC_DEBUG_MSG_STD(_T("String (") + strEx::itos(str.length()) + _T(") to long to fit inside buffer(") + strEx::itos(bufLen) + _T(") : ") + sstr);
183                                return NSCAPI::isInvalidBufferLen;
184                        }
185                        wcsncpy(buffer, str.c_str(), bufLen);
186                        return defaultReturnCode;
187                }
188
189        };
190        template<class impl_class>
191        struct basic_wrapper_static {
192
193                static int NSModuleHelperInit(nscapi::core_api::lpNSAPILoader f) {
194                        try {
195                                return nscapi::plugin_singleton->get_core()->load_endpoints(f)?NSCAPI::isSuccess:NSCAPI::hasFailed;
196                        } catch (...) {
197                                NSC_LOG_CRITICAL(_T("Unknown exception in: wrapModuleHelperInit"));
198                                return NSCAPI::hasFailed;
199                        }
200                }
201                static int NSLoadModule() {
202                        return NSCAPI::hasFailed;
203                }
204                static int NSGetModuleName(wchar_t* buf, int buflen) {
205                        try {
206                                return helpers::wrap_string(buf, buflen, impl_class::getModuleName(), NSCAPI::isSuccess);
207                        } catch (...) {
208                                NSC_LOG_CRITICAL(_T("Unknown exception in: NSGetModuleName"));
209                        }
210                        return NSCAPI::hasFailed;
211                }
212                static int NSGetModuleDescription(wchar_t* buf, int buflen) {
213                        try {
214                                return helpers::wrap_string(buf, buflen, impl_class::getModuleDescription(), NSCAPI::isSuccess);
215                        } catch (...) {
216                                NSC_LOG_CRITICAL(_T("Unknown exception in: NSGetModuleDescription"));
217                        }
218                        return NSCAPI::hasFailed;
219                }
220                static int NSGetModuleVersion(int *major, int *minor, int *revision) {
221                        try {
222                                nscapi::plugin_wrapper::module_version version = impl_class::getModuleVersion();
223                                *major = version.major;
224                                *minor = version.minor;
225                                *revision = version.revision;
226                                return NSCAPI::isSuccess;
227                        } catch (...) {
228                                NSC_LOG_CRITICAL(_T("Unknown exception in: NSGetModuleVersion"));
229                        }
230                        return NSCAPI::hasFailed;
231                }
232                static void NSDeleteBuffer(char** buffer) {
233                        try {
234                                delete [] *buffer;
235                        } catch (...) {
236                                NSC_LOG_CRITICAL(_T("Unknown exception in: NSDeleteBuffer"));
237                        }
238                }
239        };
240
241        template<class impl_class>
242        struct basic_wrapper {
243                boost::shared_ptr<impl_class> instance;
244                basic_wrapper(boost::shared_ptr<impl_class> instance) : instance(instance) {}
245                int NSLoadModuleEx(unsigned int id, wchar_t* alias, int mode) {
246                        try {
247                                instance->set_id(id);
248                                if (instance->loadModuleEx(alias, mode))
249                                        return NSCAPI::isSuccess;
250                        } catch (...) {
251                                NSC_LOG_CRITICAL(_T("Unknown exception in: NSLoadModuleEx"));
252                        }
253                        return NSCAPI::hasFailed;
254                }
255                int NSUnloadModule() {
256                        try {
257                                if (instance->unloadModule())
258                                        return NSCAPI::isSuccess;
259                        } catch (...) {
260                                NSC_LOG_CRITICAL(_T("Unknown exception in: NSUnloadModule"));
261                        }
262                        return NSCAPI::hasFailed;
263                }
264        };
265        template<class impl_class>
266        struct message_wrapper {
267                boost::shared_ptr<impl_class> instance;
268                message_wrapper(boost::shared_ptr<impl_class> instance) : instance(instance) {}
269                void NSHandleMessage(const char* request_buffer, unsigned int request_buffer_len) {
270                        try {
271                                instance->handleMessageRAW(std::string(request_buffer, request_buffer_len));
272                        } catch (...) {
273                                NSC_LOG_CRITICAL(_T("Unknown exception in: NSHandleMessage"));
274                        }
275                }
276                NSCAPI::boolReturn NSHasMessageHandler() {
277                        try {
278                                if (instance->hasMessageHandler())
279                                        return NSCAPI::istrue;
280                        } catch (...) {
281                                NSC_LOG_CRITICAL(_T("Unknown exception in: NSHasMessageHandler"));
282                        }
283                        return NSCAPI::isfalse;
284                }
285        };
286        template<class impl_class>
287        struct command_wrapper {
288                boost::shared_ptr<impl_class> instance;
289                command_wrapper(boost::shared_ptr<impl_class> instance) : instance(instance) {}
290
291                NSCAPI::nagiosReturn NSHandleCommand(const wchar_t* command, const char* request_buffer, const unsigned int request_buffer_len, char** reply_buffer, unsigned int *reply_buffer_len) {
292                        try {
293                                std::string request(request_buffer, request_buffer_len), reply;
294                                NSCAPI::nagiosReturn retCode = instance->handleRAWCommand(command, request, reply);
295                                helpers::wrap_string(reply, reply_buffer, reply_buffer_len);
296                                if (!nscapi::plugin_helper::isMyNagiosReturn(retCode)) {
297                                        NSC_LOG_ERROR(_T("A module returned an invalid return code"));
298                                }
299                                return retCode;
300                        } catch (...) {
301                                NSC_LOG_CRITICAL(_T("Unknown exception in: NSHandleCommand"));
302                        }
303                        return NSCAPI::returnIgnored;
304                }
305                NSCAPI::boolReturn NSHasCommandHandler() {
306                        try {
307                                if (instance->hasCommandHandler())
308                                        return NSCAPI::istrue;
309                        } catch (...) {
310                                NSC_LOG_CRITICAL(_T("Unknown exception in: NSHasCommandHandler"));
311                        }
312                        return NSCAPI::isfalse;
313                }
314        };
315
316        template<class impl_class>
317        struct routing_wrapper {
318                boost::shared_ptr<impl_class> instance;
319                routing_wrapper(boost::shared_ptr<impl_class> instance) : instance(instance) {}
320
321                NSCAPI::nagiosReturn NSRouteMessage(const wchar_t* channel, const wchar_t* command, const char* request_buffer, const unsigned int request_buffer_len, char** reply_buffer, unsigned int *reply_buffer_len) {
322                        try {
323                                std::string request(request_buffer, request_buffer_len), reply;
324                                NSCAPI::nagiosReturn retCode = instance->RAWRouteMessage(channel, command, request, reply);
325                                helpers::wrap_string(reply, reply_buffer, reply_buffer_len);
326                                return retCode;
327                        } catch (...) {
328                                NSC_LOG_CRITICAL(_T("Unknown exception in: NSRouteMessage"));
329                        }
330                        return NSCAPI::returnIgnored;
331                }
332                NSCAPI::boolReturn NSHasRoutingHandler() {
333                        try {
334                                if (instance->hasRoutingHandler())
335                                        return NSCAPI::istrue;
336                        } catch (...) {
337                                NSC_LOG_CRITICAL(_T("Unknown exception in: NSHasRoutingHandler"));
338                        }
339                        return NSCAPI::isfalse;
340                }
341        };
342
343        template<class impl_class>
344        struct submission_wrapper {
345                boost::shared_ptr<impl_class> instance;
346                submission_wrapper(boost::shared_ptr<impl_class> instance) : instance(instance) {}
347
348                NSCAPI::nagiosReturn NSHandleNotification(const wchar_t* channel, const char* buffer, unsigned int buffer_len, char** response_buffer, unsigned int *response_buffer_len) {
349                        try {
350                                std::string request(buffer, buffer_len), reply;
351                                NSCAPI::nagiosReturn retCode = instance->handleRAWNotification(channel, request, reply);
352                                helpers::wrap_string(reply, response_buffer, response_buffer_len);
353                                //return helpers::wrap_string(reply, response_buffer, response_buffer_len, retCode);
354                                return retCode;
355                        } catch (...) {
356                                NSC_LOG_CRITICAL(_T("Unknown exception in: NSHandleNotification"));
357                        }
358                        return NSCAPI::returnIgnored;
359                }
360                NSCAPI::boolReturn NSHasNotificationHandler() {
361                        try {
362                                if (instance->hasNotificationHandler())
363                                        return NSCAPI::istrue;
364                        } catch (...) {
365                                NSC_LOG_CRITICAL(_T("Unknown exception in: NSHasNotificationHandler"));
366                        }
367                        return NSCAPI::isfalse;
368                }
369        };
370
371        template<class impl_class>
372        struct cliexec_wrapper {
373                boost::shared_ptr<impl_class> instance;
374                cliexec_wrapper(boost::shared_ptr<impl_class> instance) : instance(instance) {}
375
376                int NSCommandLineExec(wchar_t *command, char *request_buffer, unsigned int request_buffer_len, char **response_buffer, unsigned int *response_buffer_len) {
377                        try {
378                                std::string request(request_buffer, request_buffer_len), reply;
379                                NSCAPI::nagiosReturn retCode = instance->commandRAWLineExec(command, request, reply);
380                                helpers::wrap_string(reply, response_buffer, response_buffer_len);
381                                return retCode;
382                        } catch (...) {
383                                NSC_LOG_CRITICAL(_T("Unknown exception in: NSCommandLineExec"));
384                        }
385                        return NSCAPI::hasFailed;
386                }
387        };
388};
Note: See TracBrowser for help on using the repository browser.