source: nscp/NSCPlugin.cpp @ 75d5e70

0.4.00.4.10.4.2stable
Last change on this file since 75d5e70 was 75d5e70, checked in by Michael Medin <michael@…>, 8 years ago

Alot of fixes and some changes (se changelog for details)

  • Property mode set to 100644
File size: 9.0 KB
Line 
1#include "stdafx.h"
2#include "NSClient++.h"
3/**
4 * Default c-tor
5 * Initializes the plug in name but does not load the actual plug in.<br>
6 * To load the plug in use function load() that loads an initializes the plug in.
7 *
8 * @param file The file (DLL) to load as a NSC plug in.
9 */
10NSCPlugin::NSCPlugin(const std::string file)
11        : file_(file)
12        ,hModule_(NULL)
13        ,fLoadModule(NULL)
14        ,fGetName(NULL)
15        ,fHasCommandHandler(NULL)
16        ,fUnLoadModule(NULL)
17        ,fHasMessageHandler(NULL)
18        ,fHandleMessage(NULL)
19        ,fGetDescription(NULL)
20        ,fGetConfigurationMeta(NULL)
21        ,fGetVersion(NULL)
22        ,bLoaded_(false)
23{
24}
25
26NSCPlugin::NSCPlugin(NSCPlugin &other)
27        :hModule_(NULL)
28        ,fLoadModule(NULL)
29        ,fGetName(NULL)
30        ,fHasCommandHandler(NULL)
31        ,fUnLoadModule(NULL)
32        ,fHasMessageHandler(NULL)
33        ,fHandleMessage(NULL)
34        ,fGetDescription(NULL)
35        ,fGetConfigurationMeta(NULL)
36        ,fGetVersion(NULL)
37        ,bLoaded_(false)
38{
39        if (other.bLoaded_) {
40                file_ = other.file_;
41                hModule_ = LoadLibrary(file_.c_str());
42                if (!hModule_)
43                        throw NSPluginException(file_, "Could not load library: ", GetLastError());
44                loadRemoteProcs_();
45                if (!fLoadModule)
46                        throw NSPluginException(file_, "Critical error (fLoadModule)");
47                bLoaded_ = other.bLoaded_;
48        }
49}
50
51/**
52 * Default d-tor
53 */
54NSCPlugin::~NSCPlugin() {
55        if (isLoaded())
56                unload();
57}
58/**
59 * Returns the name of the plug in.
60 *
61 * @return Name of the plug in.
62 *
63 * @throws NSPluginException if the module is not loaded.
64 */
65std::string NSCPlugin::getName() {
66        char *buffer = new char[1024];
67        if (!getName_(buffer, 1023)) {
68                throw NSPluginException(file_, "Could not get name");
69        }
70        std::string ret = buffer;
71        delete [] buffer;
72        return ret;
73}
74std::string NSCPlugin::getDescription() {
75        char *buffer = new char[4096];
76        if (!getDescription_(buffer, 4095)) {
77                throw NSPluginException(file_, "Could not get description");
78        }
79        std::string ret = buffer;
80        delete [] buffer;
81        return ret;
82}
83
84/**
85 * Loads the plug in (DLL) and initializes the plug in by calling NSLoadModule
86 *
87 * @throws NSPluginException when exceptions occur.
88 * Exceptions include but are not limited to: DLL fails to load, DLL is not a correct plug in.
89 */
90void NSCPlugin::load_dll() {
91        if (isLoaded())
92                throw NSPluginException(file_, "Module already loaded");
93        hModule_ = LoadLibrary(file_.c_str());
94        if (!hModule_)
95                throw NSPluginException(file_, "Could not load library: ", GetLastError());
96        loadRemoteProcs_();
97        bLoaded_ = true;
98}
99
100void NSCPlugin::load_plugin() {
101        if (!fLoadModule)
102                throw NSPluginException(file_, "Critical error (fLoadModule)");
103        if (!fLoadModule())
104                throw NSPluginException(file_, "Could not load plug in");
105}
106
107
108/**
109 * Get the plug in version.
110 *
111 * @bug Not implemented as of yet
112 *
113 * @param *major
114 * @param *minor
115 * @param *revision
116 * @return False
117 */
118bool NSCPlugin::getVersion(int *major, int *minor, int *revision) {
119        if (!isLoaded())
120                throw NSPluginException(file_, "Library is not loaded");
121        if (!fGetVersion)
122                throw NSPluginException(file_, "Critical error (fGetVersion)");
123        return fGetVersion(major, minor, revision)?true:false;
124}
125/**
126 * Returns true if the plug in has a command handler.
127 * @return true if the plug in has a command handler.
128 * @throws NSPluginException if the module is not loaded.
129 */
130bool NSCPlugin::hasCommandHandler() {
131        if (!isLoaded())
132                throw NSPluginException(file_, "Module not loaded");
133        if (fHasCommandHandler())
134                return true;
135        return false;
136}
137/**
138* Returns true if the plug in has a message (log) handler.
139* @return true if the plug in has a message (log) handler.
140* @throws NSPluginException if the module is not loaded.
141*/
142bool NSCPlugin::hasMessageHandler() {
143        if (!isLoaded())
144                throw NSPluginException(file_, "Module not loaded");
145        if (fHasMessageHandler())
146                return true;
147        return false;
148}
149/**
150 * Allow for the plug in to handle a command from the input core.
151 *
152 * Plug ins may refuse to handle the plug in (if not applicable) by returning an empty string.
153 *
154 * @param command The command name (is a string encoded number for legacy commands)
155 * @param argLen The length of the argument buffer.
156 * @param **arguments The arguments for this command
157 * @param returnMessageBuffer Return buffer for plug in to store the result of the executed command.
158 * @param returnMessageBufferLen Size of returnMessageBuffer
159 * @param returnPerfBuffer Return buffer for performance data
160 * @param returnPerfBufferLen Sixe of returnPerfBuffer
161 * @return Status of execution. Could be error codes, buffer length messages etc.
162 * @throws NSPluginException if the module is not loaded.
163 */
164NSCAPI::nagiosReturn NSCPlugin::handleCommand(const char* command, const unsigned int argLen, char **arguments, char* returnMessageBuffer, unsigned int returnMessageBufferLen, char* returnPerfBuffer, unsigned int returnPerfBufferLen) {
165        if (!isLoaded())
166                throw NSPluginException(file_, "Library is not loaded");
167        return fHandleCommand(command, argLen, arguments, returnMessageBuffer, returnMessageBufferLen, returnPerfBuffer, returnPerfBufferLen);
168}
169/**
170 * Handle a message from the core (or any other (or even potentially self) plug in).
171 * A message may be anything really errors, log messages etc.
172 *
173 * @param msgType Type of message (error, warning, debug, etc.)
174 * @param file The file that generated this message generally __FILE__.
175 * @param line The line in the file that generated the message generally __LINE__
176 * @throws NSPluginException if the module is not loaded.
177 */
178void NSCPlugin::handleMessage(int msgType, const char* file, const int line, const char *message) {
179        if (!fHandleMessage)
180                throw NSPluginException(file_, "Library is not loaded");
181        fHandleMessage(msgType, file, line, message);
182}
183/**
184 * Unload the plug in
185 * @throws NSPluginException if the module is not loaded and/or cannot be unloaded (plug in remains loaded if so).
186 */
187void NSCPlugin::unload() {
188        if (!isLoaded())
189                throw NSPluginException(file_, "Library is not loaded");
190        if (!fUnLoadModule)
191                throw NSPluginException(file_, "Critical error (fUnLoadModule)");
192        fUnLoadModule();
193        FreeLibrary(hModule_);
194        hModule_ = NULL;
195        bLoaded_ = false;
196}
197bool NSCPlugin::getName_(char* buf, unsigned int buflen) {
198        if (fGetName == NULL)
199                throw NSPluginException(file_, "Critical error (fGetName)");
200        return fGetName(buf, buflen)?true:false;
201}
202bool NSCPlugin::getDescription_(char* buf, unsigned int buflen) {
203        if (fGetDescription == NULL)
204                throw NSPluginException(file_, "Critical error (fGetDescription)");
205        return fGetDescription(buf, buflen)?true:false;
206}
207/**
208 * Load all remote function pointers from the loaded module.
209 * These pointers are cached for "speed" which might (?) be dangerous if something changes.
210 * @throws NSPluginException if any of the function pointers fail to load.
211 * If NSPluginException  is thrown the loaded might remain partially loaded and crashes might occur if plug in is used in this state.
212 */
213void NSCPlugin::loadRemoteProcs_(void) {
214
215        fLoadModule = (lpLoadModule)GetProcAddress(hModule_, "NSLoadModule");
216        if (!fLoadModule)
217                throw NSPluginException(file_, "Could not load NSLoadModule");
218
219        fModuleHelperInit = (lpModuleHelperInit)GetProcAddress(hModule_, "NSModuleHelperInit");
220        if (!fModuleHelperInit)
221                throw NSPluginException(file_, "Could not load NSModuleHelperInit");
222
223        fModuleHelperInit(NSAPILoader);
224       
225        fGetName = (lpGetName)GetProcAddress(hModule_, "NSGetModuleName");
226        if (!fGetName)
227                throw NSPluginException(file_, "Could not load NSGetModuleName");
228
229        fGetVersion = (lpGetVersion)GetProcAddress(hModule_, "NSGetModuleVersion");
230        if (!fGetVersion)
231                throw NSPluginException(file_, "Could not load NSGetModuleVersion");
232
233        fGetDescription = (lpGetDescription)GetProcAddress(hModule_, "NSGetModuleDescription");
234        if (!fGetDescription)
235                throw NSPluginException(file_, "Could not load NSGetModuleDescription");
236
237        fHasCommandHandler = (lpHasCommandHandler)GetProcAddress(hModule_, "NSHasCommandHandler");
238        if (!fHasCommandHandler)
239                throw NSPluginException(file_, "Could not load NSHasCommandHandler");
240
241        fHasMessageHandler = (lpHasMessageHandler)GetProcAddress(hModule_, "NSHasMessageHandler");
242        if (!fHasMessageHandler)
243                throw NSPluginException(file_, "Could not load NSHasMessageHandler");
244
245        fHandleCommand = (lpHandleCommand)GetProcAddress(hModule_, "NSHandleCommand");
246        if (!fHandleCommand)
247                throw NSPluginException(file_, "Could not load NSHandleCommand");
248
249        fHandleMessage = (lpHandleMessage)GetProcAddress(hModule_, "NSHandleMessage");
250        if (!fHandleMessage)
251                throw NSPluginException(file_, "Could not load NSHandleMessage");
252
253        fUnLoadModule = (lpUnLoadModule)GetProcAddress(hModule_, "NSUnloadModule");
254        if (!fUnLoadModule)
255                throw NSPluginException(file_, "Could not load NSUnloadModule");
256
257        fGetConfigurationMeta = (lpGetConfigurationMeta)GetProcAddress(hModule_, "NSGetConfigurationMeta");
258}
259
260
261std::string NSCPlugin::getCongifurationMeta()
262{
263        char *buffer = new char[4097];
264        if (!getConfigurationMeta_(buffer, 4096)) {
265                throw NSPluginException(file_, "Could not get metadata");
266        }
267        std::string ret = buffer;
268        delete [] buffer;
269        return ret;
270}
271bool NSCPlugin::getConfigurationMeta_(char* buf, unsigned int buflen) {
272        if (fGetConfigurationMeta == NULL)
273                throw NSPluginException(file_, "Critical error (getCongifurationMeta)");
274        return fGetConfigurationMeta(buflen, buf)?true:false;
275}
Note: See TracBrowser for help on using the repository browser.