| 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 | |
|---|
| 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 { |
|---|
| 53 | public: |
|---|
| 54 | std::string file_; // DLL filename (for which the exception was thrown) |
|---|
| 55 | std::string error_; // An error message (human readable format) |
|---|
| 56 | /** |
|---|
| 57 | * @param file DLL filename (for which the exception is thrown) |
|---|
| 58 | * @param error An error message (human readable format) |
|---|
| 59 | */ |
|---|
| 60 | NSPluginException(std::string file, std::string error) : file_(file), error_(error) { |
|---|
| 61 | } |
|---|
| 62 | /** |
|---|
| 63 | * |
|---|
| 64 | * @param file DLL filename (for which the exception is thrown) |
|---|
| 65 | * @param sError An error message (human readable format) |
|---|
| 66 | * @param nError Error code to be appended at the end of the string |
|---|
| 67 | * @todo Change this to be some form of standard error code and merge with above. |
|---|
| 68 | */ |
|---|
| 69 | NSPluginException(std::string file, std::string sError, int nError) : file_(file) { |
|---|
| 70 | std::stringstream s; |
|---|
| 71 | s << sError; |
|---|
| 72 | s << nError; |
|---|
| 73 | error_ = s.str(); |
|---|
| 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 | HMODULE hModule_; // module handle to the DLL (once it is loaded) |
|---|
| 109 | std::string file_; // Name of the DLL file |
|---|
| 110 | |
|---|
| 111 | typedef INT (*lpModuleHelperInit)(NSCModuleHelper::lpNSAPILoader f); |
|---|
| 112 | typedef INT (*lpLoadModule)(); |
|---|
| 113 | typedef INT (*lpGetName)(char*,unsigned int); |
|---|
| 114 | typedef INT (*lpGetDescription)(char*,unsigned int); |
|---|
| 115 | typedef INT (*lpGetVersion)(int*,int*,int*); |
|---|
| 116 | typedef INT (*lpHasCommandHandler)(); |
|---|
| 117 | typedef INT (*lpHasMessageHandler)(); |
|---|
| 118 | typedef NSCAPI::nagiosReturn (*lpHandleCommand)(const char*,const unsigned int, char**,char*,unsigned int,char *,unsigned int); |
|---|
| 119 | typedef INT (*lpCommandLineExec)(const char*,const unsigned int,char**); |
|---|
| 120 | typedef INT (*lpHandleMessage)(int,const char*,const int,const char*); |
|---|
| 121 | typedef INT (*lpUnLoadModule)(); |
|---|
| 122 | typedef INT (*lpGetConfigurationMeta)(int, char*); |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | lpModuleHelperInit fModuleHelperInit; |
|---|
| 126 | lpLoadModule fLoadModule; |
|---|
| 127 | lpGetName fGetName; |
|---|
| 128 | lpGetVersion fGetVersion; |
|---|
| 129 | lpGetDescription fGetDescription; |
|---|
| 130 | lpHasCommandHandler fHasCommandHandler; |
|---|
| 131 | lpHasMessageHandler fHasMessageHandler; |
|---|
| 132 | lpHandleCommand fHandleCommand; |
|---|
| 133 | lpHandleMessage fHandleMessage; |
|---|
| 134 | lpUnLoadModule fUnLoadModule; |
|---|
| 135 | lpGetConfigurationMeta fGetConfigurationMeta; |
|---|
| 136 | lpCommandLineExec fCommandLineExec; |
|---|
| 137 | |
|---|
| 138 | public: |
|---|
| 139 | NSCPlugin(const std::string file); |
|---|
| 140 | NSCPlugin(NSCPlugin &other); |
|---|
| 141 | virtual ~NSCPlugin(void); |
|---|
| 142 | |
|---|
| 143 | std::string getName(void); |
|---|
| 144 | std::string getDescription(); |
|---|
| 145 | void load_dll(void); |
|---|
| 146 | void load_plugin(void); |
|---|
| 147 | bool getVersion(int *major, int *minor, int *revision); |
|---|
| 148 | bool hasCommandHandler(void); |
|---|
| 149 | bool hasMessageHandler(void); |
|---|
| 150 | NSCAPI::nagiosReturn handleCommand(const char *command, const unsigned int argLen, char **arguments, char* returnMessageBuffer, unsigned int returnMessageBufferLen, char* returnPerfBuffer, unsigned int returnPerfBufferLen); |
|---|
| 151 | void handleMessage(int msgType, const char* file, const int line, const char *message); |
|---|
| 152 | void unload(void); |
|---|
| 153 | std::string getCongifurationMeta(); |
|---|
| 154 | int commandLineExec(const char* command, const unsigned int argLen, char **arguments); |
|---|
| 155 | std::string getModule() { |
|---|
| 156 | if (file_.empty()) |
|---|
| 157 | return ""; |
|---|
| 158 | std::string ret = file_; |
|---|
| 159 | int pos = ret.find_last_of("\\"); |
|---|
| 160 | if (pos != std::string::npos && ++pos < ret.length()) { |
|---|
| 161 | ret = ret.substr(pos); |
|---|
| 162 | } |
|---|
| 163 | pos = ret.find_last_of("."); |
|---|
| 164 | if (pos != std::string::npos) { |
|---|
| 165 | ret = ret.substr(0, pos); |
|---|
| 166 | } |
|---|
| 167 | return ret; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | private: |
|---|
| 171 | bool isLoaded() const { |
|---|
| 172 | return bLoaded_; |
|---|
| 173 | } |
|---|
| 174 | bool getName_(char* buf, unsigned int buflen); |
|---|
| 175 | bool getDescription_(char* buf, unsigned int buflen); |
|---|
| 176 | void loadRemoteProcs_(void); |
|---|
| 177 | bool getConfigurationMeta_(char* buf, unsigned int buflen); |
|---|
| 178 | }; |
|---|
| 179 | |
|---|
| 180 | |
|---|