| 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 <NSCAPI.h> |
|---|
| 24 | #include <dll/dll.hpp> |
|---|
| 25 | #include <nsclient/logger.hpp> |
|---|
| 26 | |
|---|
| 27 | /** |
|---|
| 28 | * @ingroup NSClient++ |
|---|
| 29 | * Exception class for the NSCPlugin class. |
|---|
| 30 | * When an unexpected error occurs in NSCPlugin this exception is thrown. |
|---|
| 31 | * |
|---|
| 32 | * @version 1.0 |
|---|
| 33 | * first version |
|---|
| 34 | * |
|---|
| 35 | * @date 02-12-2005 |
|---|
| 36 | * |
|---|
| 37 | * @author mickem |
|---|
| 38 | * |
|---|
| 39 | * @par license |
|---|
| 40 | * This code is absolutely free to use and modify. The code is provided "as is" with |
|---|
| 41 | * no expressed or implied warranty. The author accepts no liability if it causes |
|---|
| 42 | * any damage to your computer, causes your pet to fall ill, increases baldness |
|---|
| 43 | * or makes your car start emitting strange noises when you start it up. |
|---|
| 44 | * This code has no bugs, just undocumented features! |
|---|
| 45 | * |
|---|
| 46 | * @todo |
|---|
| 47 | * Add status codes to make error type simpler to parse out. |
|---|
| 48 | * |
|---|
| 49 | * @bug |
|---|
| 50 | * |
|---|
| 51 | */ |
|---|
| 52 | class NSPluginException : public std::exception { |
|---|
| 53 | public: |
|---|
| 54 | std::wstring file_; // DLL filename (for which the exception was thrown) |
|---|
| 55 | std::wstring error_; // An error message (human readable format) |
|---|
| 56 | std::string msg_; |
|---|
| 57 | /** |
|---|
| 58 | * @param file DLL filename (for which the exception is thrown) |
|---|
| 59 | * @param error An error message (human readable format) |
|---|
| 60 | */ |
|---|
| 61 | NSPluginException(dll::dll &module, std::wstring error) : error_(error) { |
|---|
| 62 | file_ = module.get_module_name(); |
|---|
| 63 | msg_ = utf8::cvt<std::string>(error_ + _T(" in file: ") + file_); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | ~NSPluginException() throw() {} |
|---|
| 67 | const char* what() const throw() { |
|---|
| 68 | return msg_.c_str(); |
|---|
| 69 | } |
|---|
| 70 | const std::wstring wwhat() const throw() { |
|---|
| 71 | return utf8::cvt<std::wstring>(msg_); |
|---|
| 72 | } |
|---|
| 73 | }; |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | * @ingroup NSClient++ |
|---|
| 77 | * NSCPlugin is a wrapper class to wrap all DLL calls and make things simple and clean inside the actual application.<br> |
|---|
| 78 | * Things tend to be one-to-one by which I mean that a call to a function here should call the corresponding function in the plug in (if loaded). |
|---|
| 79 | * If things are "broken" NSPluginException is called to indicate this. Error states are returned for normal "conditions". |
|---|
| 80 | * |
|---|
| 81 | * |
|---|
| 82 | * @version 1.0 |
|---|
| 83 | * first version |
|---|
| 84 | * |
|---|
| 85 | * @date 02-12-2005 |
|---|
| 86 | * |
|---|
| 87 | * @author mickem |
|---|
| 88 | * |
|---|
| 89 | * @par license |
|---|
| 90 | * This code is absolutely free to use and modify. The code is provided "as is" with |
|---|
| 91 | * no expressed or implied warranty. The author accepts no liability if it causes |
|---|
| 92 | * any damage to your computer, causes your pet to fall ill, increases baldness |
|---|
| 93 | * or makes your car start emitting strange noises when you start it up. |
|---|
| 94 | * This code has no bugs, just undocumented features! |
|---|
| 95 | * |
|---|
| 96 | * @todo |
|---|
| 97 | * getVersion() is not implemented as of yet. |
|---|
| 98 | * |
|---|
| 99 | * @bug |
|---|
| 100 | * |
|---|
| 101 | */ |
|---|
| 102 | class NSCPlugin : public boost::noncopyable, public nsclient::logging::raw_subscriber { |
|---|
| 103 | private: |
|---|
| 104 | //bool bLoaded_; // Status of plug in |
|---|
| 105 | dll::dll module_; |
|---|
| 106 | bool broken_; |
|---|
| 107 | unsigned int plugin_id_; |
|---|
| 108 | std::wstring alias_; |
|---|
| 109 | bool loaded_; |
|---|
| 110 | |
|---|
| 111 | nscapi::plugin_api::lpModuleHelperInit fModuleHelperInit; |
|---|
| 112 | nscapi::plugin_api::lpLoadModule fLoadModule; |
|---|
| 113 | nscapi::plugin_api::lpGetName fGetName; |
|---|
| 114 | nscapi::plugin_api::lpGetVersion fGetVersion; |
|---|
| 115 | nscapi::plugin_api::lpGetDescription fGetDescription; |
|---|
| 116 | nscapi::plugin_api::lpHasCommandHandler fHasCommandHandler; |
|---|
| 117 | nscapi::plugin_api::lpHasMessageHandler fHasMessageHandler; |
|---|
| 118 | nscapi::plugin_api::lpHandleCommand fHandleCommand; |
|---|
| 119 | nscapi::plugin_api::lpDeleteBuffer fDeleteBuffer; |
|---|
| 120 | nscapi::plugin_api::lpHandleMessage fHandleMessage; |
|---|
| 121 | nscapi::plugin_api::lpUnLoadModule fUnLoadModule; |
|---|
| 122 | nscapi::plugin_api::lpCommandLineExec fCommandLineExec; |
|---|
| 123 | nscapi::plugin_api::lpHasNotificationHandler fHasNotificationHandler; |
|---|
| 124 | nscapi::plugin_api::lpHandleNotification fHandleNotification; |
|---|
| 125 | nscapi::plugin_api::lpHasRoutingHandler fHasRoutingHandler; |
|---|
| 126 | nscapi::plugin_api::lpRouteMessage fRouteMessage; |
|---|
| 127 | |
|---|
| 128 | public: |
|---|
| 129 | NSCPlugin(const unsigned int id, const boost::filesystem::wpath file, std::wstring alias); |
|---|
| 130 | virtual ~NSCPlugin(void); |
|---|
| 131 | |
|---|
| 132 | std::wstring getName(void); |
|---|
| 133 | std::wstring getDescription(); |
|---|
| 134 | void load_dll(); |
|---|
| 135 | bool load_plugin(NSCAPI::moduleLoadMode mode); |
|---|
| 136 | void setBroken(bool broken); |
|---|
| 137 | bool isBroken(); |
|---|
| 138 | bool getVersion(int *major, int *minor, int *revision); |
|---|
| 139 | bool hasCommandHandler(void); |
|---|
| 140 | bool hasNotificationHandler(void); |
|---|
| 141 | bool hasMessageHandler(void); |
|---|
| 142 | NSCAPI::nagiosReturn handleCommand(const wchar_t *command, const char* dataBuffer, const unsigned int dataBuffer_len, char** returnBuffer, unsigned int *returnBuffer_len); |
|---|
| 143 | NSCAPI::nagiosReturn handleCommand(const wchar_t* command, std::string &request, std::string &reply); |
|---|
| 144 | NSCAPI::nagiosReturn handleNotification(const wchar_t *channel, std::string &request, std::string &reply); |
|---|
| 145 | NSCAPI::nagiosReturn handleNotification(const wchar_t *channel, const char* request_buffer, const unsigned int request_buffer_len, char** response_buffer, unsigned int *response_buffer_len); |
|---|
| 146 | void deleteBuffer(char**buffer); |
|---|
| 147 | void handleMessage(const char* data, unsigned int len); |
|---|
| 148 | void unload_dll(void); |
|---|
| 149 | void unload_plugin(void); |
|---|
| 150 | std::wstring getCongifurationMeta(); |
|---|
| 151 | int commandLineExec(const wchar_t* command, std::string &request, std::string &reply); |
|---|
| 152 | int commandLineExec(const wchar_t* command, const char* request, const unsigned int request_len, char** reply, unsigned int *reply_len); |
|---|
| 153 | bool has_command_line_exec(); |
|---|
| 154 | bool is_duplicate( boost::filesystem::wpath file, std::wstring alias ); |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | bool has_routing_handler(); |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | bool route_message(const wchar_t *channel, const char* buffer, unsigned int buffer_len, wchar_t **new_channel_buffer, char **new_buffer, unsigned int *new_buffer_len); |
|---|
| 161 | |
|---|
| 162 | std::wstring get_description() { |
|---|
| 163 | if (alias_.empty()) |
|---|
| 164 | return getName(); |
|---|
| 165 | return getName() + _T(" (") + alias_ + _T(")"); |
|---|
| 166 | } |
|---|
| 167 | std::wstring get_alias() { |
|---|
| 168 | return alias_; |
|---|
| 169 | } |
|---|
| 170 | std::wstring getFilename() { |
|---|
| 171 | return module_.get_filename(); |
|---|
| 172 | } |
|---|
| 173 | std::wstring getModule() { |
|---|
| 174 | #ifndef WIN32 |
|---|
| 175 | std::wstring file = module_.get_module_name(); |
|---|
| 176 | if (file.substr(0,3) == _T("lib")) |
|---|
| 177 | file = file.substr(3); |
|---|
| 178 | return file; |
|---|
| 179 | #else |
|---|
| 180 | return module_.get_module_name(); |
|---|
| 181 | #endif |
|---|
| 182 | } |
|---|
| 183 | static std::wstring get_plugin_file(std::wstring key) { |
|---|
| 184 | #ifdef WIN32 |
|---|
| 185 | return key + _T(".dll"); |
|---|
| 186 | #else |
|---|
| 187 | return _T("lib") + key + _T(".so"); |
|---|
| 188 | #endif |
|---|
| 189 | } |
|---|
| 190 | bool getLastIsMsgPlugin() { |
|---|
| 191 | return lastIsMsgPlugin_; |
|---|
| 192 | } |
|---|
| 193 | bool isLoaded() const { |
|---|
| 194 | return module_.is_loaded(); |
|---|
| 195 | } |
|---|
| 196 | unsigned int get_id() const { return plugin_id_; } |
|---|
| 197 | static boost::filesystem::wpath get_filename(boost::filesystem::wpath folder, std::wstring module); |
|---|
| 198 | |
|---|
| 199 | public: |
|---|
| 200 | void on_raw_log_message(std::string &payload) { |
|---|
| 201 | handleMessage(payload.c_str(), payload.size()); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | private: |
|---|
| 205 | bool lastIsMsgPlugin_; |
|---|
| 206 | bool getName_(wchar_t* buf, unsigned int buflen); |
|---|
| 207 | bool getDescription_(wchar_t* buf, unsigned int buflen); |
|---|
| 208 | void loadRemoteProcs_(void); |
|---|
| 209 | }; |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|