| 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 | */ |
|---|
| 10 | NSCPlugin::NSCPlugin(const std::string file) |
|---|
| 11 | : file_(file) |
|---|
| 12 | ,fLoadModule(NULL) |
|---|
| 13 | ,fGetName(NULL) |
|---|
| 14 | ,fHasCommandHandler(NULL) |
|---|
| 15 | ,fUnLoadModule(NULL) |
|---|
| 16 | ,fHasMessageHandler(NULL) |
|---|
| 17 | ,fHandleMessage(NULL) |
|---|
| 18 | ,bLoaded_(false) |
|---|
| 19 | { |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | /** |
|---|
| 23 | * Default d-tor |
|---|
| 24 | */ |
|---|
| 25 | NSCPlugin::~NSCPlugin() { |
|---|
| 26 | if (isLoaded()) |
|---|
| 27 | unload(); |
|---|
| 28 | } |
|---|
| 29 | /** |
|---|
| 30 | * Returns the name of the plug in. |
|---|
| 31 | * |
|---|
| 32 | * @return Name of the plug in. |
|---|
| 33 | * |
|---|
| 34 | * @throws NSPluginException if the module is not loaded. |
|---|
| 35 | */ |
|---|
| 36 | std::string NSCPlugin::getName() { |
|---|
| 37 | char *buffer = new char[1024]; |
|---|
| 38 | if (!getName_(buffer, 1023)) { |
|---|
| 39 | throw NSPluginException(file_, "Could not get name"); |
|---|
| 40 | } |
|---|
| 41 | std::string ret = buffer; |
|---|
| 42 | delete [] buffer; |
|---|
| 43 | return ret; |
|---|
| 44 | } |
|---|
| 45 | /** |
|---|
| 46 | * Loads the plug in (DLL) and initializes the plug in by calling NSLoadModule |
|---|
| 47 | * |
|---|
| 48 | * @throws NSPluginException when exceptions occur. |
|---|
| 49 | * Exceptions include but are not limited to: DLL fails to load, DLL is not a correct plug in. |
|---|
| 50 | */ |
|---|
| 51 | void NSCPlugin::load() { |
|---|
| 52 | if (isLoaded()) |
|---|
| 53 | throw NSPluginException(file_, "Module already loaded"); |
|---|
| 54 | hModule_ = LoadLibrary(file_.c_str()); |
|---|
| 55 | if (!hModule_) |
|---|
| 56 | throw NSPluginException(file_, "Could not load library: ", GetLastError()); |
|---|
| 57 | loadRemoteProcs_(); |
|---|
| 58 | if (!fLoadModule) |
|---|
| 59 | throw NSPluginException(file_, "Critical error (fLoadModule)"); |
|---|
| 60 | if (!fLoadModule()) |
|---|
| 61 | throw NSPluginException(file_, "Could not load plug in"); |
|---|
| 62 | bLoaded_ = true; |
|---|
| 63 | } |
|---|
| 64 | /** |
|---|
| 65 | * Get the plug in version. |
|---|
| 66 | * |
|---|
| 67 | * @bug Not implemented as of yet |
|---|
| 68 | * |
|---|
| 69 | * @param *major |
|---|
| 70 | * @param *minor |
|---|
| 71 | * @param *revision |
|---|
| 72 | * @return False |
|---|
| 73 | */ |
|---|
| 74 | bool NSCPlugin::getVersion(int *major, int *minor, int *revision) { |
|---|
| 75 | return false; |
|---|
| 76 | } |
|---|
| 77 | /** |
|---|
| 78 | * Returns true if the plug in has a command handler. |
|---|
| 79 | * @return true if the plug in has a command handler. |
|---|
| 80 | * @throws NSPluginException if the module is not loaded. |
|---|
| 81 | */ |
|---|
| 82 | bool NSCPlugin::hasCommandHandler() { |
|---|
| 83 | if (!isLoaded()) |
|---|
| 84 | throw NSPluginException(file_, "Module not loaded"); |
|---|
| 85 | if (fHasCommandHandler()) |
|---|
| 86 | return true; |
|---|
| 87 | return false; |
|---|
| 88 | } |
|---|
| 89 | /** |
|---|
| 90 | * Returns true if the plug in has a message (log) handler. |
|---|
| 91 | * @return true if the plug in has a message (log) handler. |
|---|
| 92 | * @throws NSPluginException if the module is not loaded. |
|---|
| 93 | */ |
|---|
| 94 | bool NSCPlugin::hasMessageHandler() { |
|---|
| 95 | if (!isLoaded()) |
|---|
| 96 | throw NSPluginException(file_, "Module not loaded"); |
|---|
| 97 | if (fHasMessageHandler()) |
|---|
| 98 | return true; |
|---|
| 99 | return false; |
|---|
| 100 | } |
|---|
| 101 | /** |
|---|
| 102 | * Allow for the plug in to handle a command from the input core. |
|---|
| 103 | * |
|---|
| 104 | * Plug ins may refuse to handle the plug in (if not applicable) by returning an empty string. |
|---|
| 105 | * |
|---|
| 106 | * @param *command The command name (is a string encoded number for legacy commands) |
|---|
| 107 | * @param argLen The length of the argument buffer. |
|---|
| 108 | * @param **arguments The arguments for this command |
|---|
| 109 | * @param returnBuffer Return buffer for plug in to store the result of the executed command. |
|---|
| 110 | * @param returnBufferLen Size of the return buffer. |
|---|
| 111 | * @return Status of execution. Could be error codes, buffer length messages etc. |
|---|
| 112 | * @throws NSPluginException if the module is not loaded. |
|---|
| 113 | * |
|---|
| 114 | * @todo Implement return status as an enum to make it simpler for clients to see potential return stats? |
|---|
| 115 | */ |
|---|
| 116 | NSCAPI::nagiosReturn NSCPlugin::handleCommand(const char *command, const unsigned int argLen, char **arguments, char* returnMessageBuffer, unsigned int returnMessageBufferLen, char* returnPerfBuffer, unsigned int returnPerfBufferLen) { |
|---|
| 117 | if (!isLoaded()) |
|---|
| 118 | throw NSPluginException(file_, "Library is not loaded"); |
|---|
| 119 | return fHandleCommand(command, argLen, arguments, returnMessageBuffer, returnMessageBufferLen, returnPerfBuffer, returnPerfBufferLen); |
|---|
| 120 | } |
|---|
| 121 | /** |
|---|
| 122 | * Handle a message from the core (or any other (or even potentially self) plug in). |
|---|
| 123 | * A message may be anything really errors, log messages etc. |
|---|
| 124 | * |
|---|
| 125 | * @param msgType Type of message (error, warning, debug, etc.) |
|---|
| 126 | * @param file The file that generated this message generally __FILE__. |
|---|
| 127 | * @param line The line in the file that generated the message generally __LINE__ |
|---|
| 128 | * @throws NSPluginException if the module is not loaded. |
|---|
| 129 | * @throws |
|---|
| 130 | */ |
|---|
| 131 | void NSCPlugin::handleMessage(int msgType, const char* file, const int line, const char *message) { |
|---|
| 132 | if (!fHandleMessage) |
|---|
| 133 | throw NSPluginException(file_, "Library is not loaded"); |
|---|
| 134 | fHandleMessage(msgType, file, line, message); |
|---|
| 135 | } |
|---|
| 136 | /** |
|---|
| 137 | * Unload the plug in |
|---|
| 138 | * @throws NSPluginException if the module is not loaded and/or cannot be unloaded (plug in remains loaded if so). |
|---|
| 139 | */ |
|---|
| 140 | void NSCPlugin::unload() { |
|---|
| 141 | if (!isLoaded()) |
|---|
| 142 | throw NSPluginException(file_, "Library is not loaded"); |
|---|
| 143 | if (!fUnLoadModule) |
|---|
| 144 | throw NSPluginException(file_, "Critical error (fUnLoadModule)"); |
|---|
| 145 | fUnLoadModule(); |
|---|
| 146 | FreeLibrary(hModule_); |
|---|
| 147 | hModule_ = NULL; |
|---|
| 148 | bLoaded_ = false; |
|---|
| 149 | } |
|---|
| 150 | bool NSCPlugin::getName_(char* buf, unsigned int buflen) { |
|---|
| 151 | if (fGetName == NULL) |
|---|
| 152 | throw NSPluginException(file_, "Critical error (fGetName)"); |
|---|
| 153 | return fGetName(buf, buflen)?true:false; |
|---|
| 154 | } |
|---|
| 155 | /** |
|---|
| 156 | * Load all remote function pointers from the loaded module. |
|---|
| 157 | * These pointers are cached for "speed" which might (?) be dangerous if something changes. |
|---|
| 158 | * @throws NSPluginException if any of the function pointers fail to load. |
|---|
| 159 | * If NSPluginException is thrown the loaded might remain partially loaded and crashes might occur if plug in is used in this state. |
|---|
| 160 | */ |
|---|
| 161 | void NSCPlugin::loadRemoteProcs_(void) { |
|---|
| 162 | |
|---|
| 163 | fLoadModule = (lpLoadModule)GetProcAddress(hModule_, "NSLoadModule"); |
|---|
| 164 | if (!fLoadModule) |
|---|
| 165 | throw NSPluginException(file_, "Could not load NSLoadModule"); |
|---|
| 166 | |
|---|
| 167 | fModuleHelperInit = (lpModuleHelperInit)GetProcAddress(hModule_, "NSModuleHelperInit"); |
|---|
| 168 | if (!fModuleHelperInit) |
|---|
| 169 | throw NSPluginException(file_, "Could not load NSModuleHelperInit"); |
|---|
| 170 | |
|---|
| 171 | fModuleHelperInit(NSAPILoader); |
|---|
| 172 | |
|---|
| 173 | fGetName = (lpGetName)GetProcAddress(hModule_, "NSGetModuleName"); |
|---|
| 174 | if (!fGetName) |
|---|
| 175 | throw NSPluginException(file_, "Could not load NSGetModuleName"); |
|---|
| 176 | |
|---|
| 177 | fHasCommandHandler = (lpHasCommandHandler)GetProcAddress(hModule_, "NSHasCommandHandler"); |
|---|
| 178 | if (!fHasCommandHandler) |
|---|
| 179 | throw NSPluginException(file_, "Could not load NSHasCommandHandler"); |
|---|
| 180 | |
|---|
| 181 | fHasMessageHandler = (lpHasMessageHandler)GetProcAddress(hModule_, "NSHasMessageHandler"); |
|---|
| 182 | if (!fHasMessageHandler) |
|---|
| 183 | throw NSPluginException(file_, "Could not load NSHasMessageHandler"); |
|---|
| 184 | |
|---|
| 185 | fHandleCommand = (lpHandleCommand)GetProcAddress(hModule_, "NSHandleCommand"); |
|---|
| 186 | if (!fHandleCommand) |
|---|
| 187 | throw NSPluginException(file_, "Could not load NSHandleCommand"); |
|---|
| 188 | |
|---|
| 189 | fHandleMessage = (lpHandleMessage)GetProcAddress(hModule_, "NSHandleMessage"); |
|---|
| 190 | if (!fHandleMessage) |
|---|
| 191 | throw NSPluginException(file_, "Could not load NSHandleMessage"); |
|---|
| 192 | |
|---|
| 193 | fUnLoadModule = (lpUnLoadModule)GetProcAddress(hModule_, "NSUnloadModule"); |
|---|
| 194 | if (!fUnLoadModule) |
|---|
| 195 | throw NSPluginException(file_, "Could not load NSUnloadModule"); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | |
|---|