| 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 <NSCHelper.h> |
|---|
| 25 | #include <sstream> |
|---|
| 26 | #include <dll/dll.hpp> |
|---|
| 27 | |
|---|
| 28 | /** |
|---|
| 29 | * @ingroup NSClient++ |
|---|
| 30 | * Exception class for the NSCPlugin class. |
|---|
| 31 | * When an unexpected error occurs in NSCPlugin this exception is thrown. |
|---|
| 32 | * |
|---|
| 33 | * @version 1.0 |
|---|
| 34 | * first version |
|---|
| 35 | * |
|---|
| 36 | * @date 02-12-2005 |
|---|
| 37 | * |
|---|
| 38 | * @author mickem |
|---|
| 39 | * |
|---|
| 40 | * @par license |
|---|
| 41 | * This code is absolutely free to use and modify. The code is provided "as is" with |
|---|
| 42 | * no expressed or implied warranty. The author accepts no liability if it causes |
|---|
| 43 | * any damage to your computer, causes your pet to fall ill, increases baldness |
|---|
| 44 | * or makes your car start emitting strange noises when you start it up. |
|---|
| 45 | * This code has no bugs, just undocumented features! |
|---|
| 46 | * |
|---|
| 47 | * @todo |
|---|
| 48 | * Add status codes to make error type simpler to parse out. |
|---|
| 49 | * |
|---|
| 50 | * @bug |
|---|
| 51 | * |
|---|
| 52 | */ |
|---|
| 53 | class NSPluginException { |
|---|
| 54 | public: |
|---|
| 55 | std::wstring file_; // DLL filename (for which the exception was thrown) |
|---|
| 56 | std::wstring error_; // An error message (human readable format) |
|---|
| 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_ = getModule(module.get_file()); |
|---|
| 63 | } |
|---|
| 64 | std::wstring getModule(std::wstring file) { |
|---|
| 65 | if (file.empty()) |
|---|
| 66 | return _T(""); |
|---|
| 67 | std::wstring ret = file; |
|---|
| 68 | std::wstring::size_type pos = ret.find_last_of(_T("\\")); |
|---|
| 69 | if (pos != std::wstring::npos && ++pos < ret.length()) { |
|---|
| 70 | ret = ret.substr(pos); |
|---|
| 71 | } |
|---|
| 72 | return ret; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | }; |
|---|
| 77 | |
|---|
| 78 | /** |
|---|
| 79 | * @ingroup NSClient++ |
|---|
| 80 | * NSCPlugin is a wrapper class to wrap all DLL calls and make things simple and clean inside the actual application.<br> |
|---|
| 81 | * 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). |
|---|
| 82 | * If things are "broken" NSPluginException is called to indicate this. Error states are returned for normal "conditions". |
|---|
| 83 | * |
|---|
| 84 | * |
|---|
| 85 | * @version 1.0 |
|---|
| 86 | * first version |
|---|
| 87 | * |
|---|
| 88 | * @date 02-12-2005 |
|---|
| 89 | * |
|---|
| 90 | * @author mickem |
|---|
| 91 | * |
|---|
| 92 | * @par license |
|---|
| 93 | * This code is absolutely free to use and modify. The code is provided "as is" with |
|---|
| 94 | * no expressed or implied warranty. The author accepts no liability if it causes |
|---|
| 95 | * any damage to your computer, causes your pet to fall ill, increases baldness |
|---|
| 96 | * or makes your car start emitting strange noises when you start it up. |
|---|
| 97 | * This code has no bugs, just undocumented features! |
|---|
| 98 | * |
|---|
| 99 | * @todo |
|---|
| 100 | * getVersion() is not implemented as of yet. |
|---|
| 101 | * |
|---|
| 102 | * @bug |
|---|
| 103 | * |
|---|
| 104 | */ |
|---|
| 105 | class NSCPlugin { |
|---|
| 106 | private: |
|---|
| 107 | bool bLoaded_; // Status of plug in |
|---|
| 108 | dll::dll module_; |
|---|
| 109 | bool broken_; |
|---|
| 110 | static unsigned int last_plugin_id_; |
|---|
| 111 | unsigned int plugin_id_; |
|---|
| 112 | |
|---|
| 113 | typedef int (*lpModuleHelperInit)(NSCModuleHelper::lpNSAPILoader f); |
|---|
| 114 | typedef int (*lpLoadModule)(int); |
|---|
| 115 | typedef int (*lpGetName)(wchar_t*,unsigned int); |
|---|
| 116 | typedef int (*lpGetDescription)(wchar_t*,unsigned int); |
|---|
| 117 | typedef int (*lpGetVersion)(int*,int*,int*); |
|---|
| 118 | typedef int (*lpHasCommandHandler)(); |
|---|
| 119 | typedef int (*lpHasMessageHandler)(); |
|---|
| 120 | typedef NSCAPI::nagiosReturn (*lpHandleCommand)(const wchar_t*,const unsigned int, wchar_t**,wchar_t*,unsigned int,wchar_t *,unsigned int); |
|---|
| 121 | typedef int (*lpCommandLineExec)(const wchar_t*,const unsigned int,wchar_t**); |
|---|
| 122 | typedef int (*lpHandleMessage)(int,const wchar_t*,const int,const wchar_t*); |
|---|
| 123 | typedef int (*lpUnLoadModule)(); |
|---|
| 124 | typedef int (*lpGetConfigurationMeta)(int, wchar_t*); |
|---|
| 125 | typedef void (*lpShowTray)(); |
|---|
| 126 | typedef void (*lpHideTray)(); |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | lpModuleHelperInit fModuleHelperInit; |
|---|
| 130 | lpLoadModule fLoadModule; |
|---|
| 131 | lpGetName fGetName; |
|---|
| 132 | lpGetVersion fGetVersion; |
|---|
| 133 | lpGetDescription fGetDescription; |
|---|
| 134 | lpHasCommandHandler fHasCommandHandler; |
|---|
| 135 | lpHasMessageHandler fHasMessageHandler; |
|---|
| 136 | lpHandleCommand fHandleCommand; |
|---|
| 137 | lpHandleMessage fHandleMessage; |
|---|
| 138 | lpUnLoadModule fUnLoadModule; |
|---|
| 139 | lpGetConfigurationMeta fGetConfigurationMeta; |
|---|
| 140 | lpCommandLineExec fCommandLineExec; |
|---|
| 141 | lpShowTray fShowTray; |
|---|
| 142 | lpHideTray fHideTray; |
|---|
| 143 | |
|---|
| 144 | public: |
|---|
| 145 | NSCPlugin(const boost::filesystem::wpath file); |
|---|
| 146 | NSCPlugin(NSCPlugin &other); |
|---|
| 147 | virtual ~NSCPlugin(void); |
|---|
| 148 | |
|---|
| 149 | std::wstring getName(void); |
|---|
| 150 | std::wstring getDescription(); |
|---|
| 151 | void load_dll(void); |
|---|
| 152 | bool load_plugin(NSCAPI::moduleLoadMode mode); |
|---|
| 153 | void setBroken(bool broken); |
|---|
| 154 | bool isBroken(); |
|---|
| 155 | bool getVersion(int *major, int *minor, int *revision); |
|---|
| 156 | bool hasCommandHandler(void); |
|---|
| 157 | bool hasMessageHandler(void); |
|---|
| 158 | NSCAPI::nagiosReturn handleCommand(const wchar_t *command, const unsigned int argLen, wchar_t **arguments, wchar_t* returnMessageBuffer, unsigned int returnMessageBufferLen, wchar_t* returnPerfBuffer, unsigned int returnPerfBufferLen); |
|---|
| 159 | void handleMessage(int msgType, const wchar_t* file, const int line, const wchar_t *message); |
|---|
| 160 | void unload(void); |
|---|
| 161 | std::wstring getCongifurationMeta(); |
|---|
| 162 | int commandLineExec(const wchar_t* command, const unsigned int argLen, wchar_t **arguments); |
|---|
| 163 | void showTray(); |
|---|
| 164 | void hideTray(); |
|---|
| 165 | |
|---|
| 166 | std::wstring getFilename() { |
|---|
| 167 | std::wstring file = module_.get_file(); |
|---|
| 168 | if (file.empty()) |
|---|
| 169 | return _T(""); |
|---|
| 170 | std::wstring::size_type pos = file.find_last_of(_T("\\")); |
|---|
| 171 | if (pos != std::wstring::npos && ++pos < file.length()) { |
|---|
| 172 | return file.substr(pos); |
|---|
| 173 | } |
|---|
| 174 | return file; |
|---|
| 175 | } |
|---|
| 176 | std::wstring getModule() { |
|---|
| 177 | std::wstring file = getFilename(); |
|---|
| 178 | std::wstring::size_type pos = file.find_last_of(_T(".")); |
|---|
| 179 | if (pos != std::wstring::npos) { |
|---|
| 180 | file = file.substr(0, pos); |
|---|
| 181 | } |
|---|
| 182 | return file; |
|---|
| 183 | } |
|---|
| 184 | bool getLastIsMsgPlugin() { |
|---|
| 185 | return lastIsMsgPlugin_; |
|---|
| 186 | } |
|---|
| 187 | bool isLoaded() const { |
|---|
| 188 | return bLoaded_; |
|---|
| 189 | } |
|---|
| 190 | unsigned int get_id() const { return plugin_id_; } |
|---|
| 191 | |
|---|
| 192 | private: |
|---|
| 193 | bool lastIsMsgPlugin_; |
|---|
| 194 | bool getName_(wchar_t* buf, unsigned int buflen); |
|---|
| 195 | bool getDescription_(wchar_t* buf, unsigned int buflen); |
|---|
| 196 | void loadRemoteProcs_(void); |
|---|
| 197 | bool getConfigurationMeta_(wchar_t* buf, unsigned int buflen); |
|---|
| 198 | }; |
|---|
| 199 | |
|---|
| 200 | unsigned int NSCPlugin::last_plugin_id_ = 0; |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | |
|---|