| 1 | #include "stdafx.h" |
|---|
| 2 | #include <NSCHelper.h> |
|---|
| 3 | |
|---|
| 4 | #define BUFF_LEN 4096 |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | /** |
|---|
| 8 | * Wrap a return string. |
|---|
| 9 | * This function copies a string to a char buffer making sure the buffer has the correct length. |
|---|
| 10 | * |
|---|
| 11 | * @param *buffer Buffer to copy the string to. |
|---|
| 12 | * @param bufLen Length of the buffer |
|---|
| 13 | * @param str Th string to copy |
|---|
| 14 | * @return NSCAPI::success unless the buffer is to short then it will be NSCAPI::invalidBufferLen |
|---|
| 15 | */ |
|---|
| 16 | int NSCHelper::wrapReturnString(char *buffer, unsigned int bufLen, std::string str, int defaultReturnCode /* = NSCAPI::success */) { |
|---|
| 17 | if (str.length() > bufLen) |
|---|
| 18 | return NSCAPI::invalidBufferLen; |
|---|
| 19 | strncpy(buffer, str.c_str(), bufLen); |
|---|
| 20 | return defaultReturnCode; |
|---|
| 21 | } |
|---|
| 22 | /** |
|---|
| 23 | * Make a list out of a array of char arrays (arguments type) |
|---|
| 24 | * @param argLen Length of argument array |
|---|
| 25 | * @param *argument[] Argument array |
|---|
| 26 | * @return Argument wrapped as a list |
|---|
| 27 | */ |
|---|
| 28 | std::list<std::string> NSCHelper::makelist(const unsigned int argLen, char *argument[]) { |
|---|
| 29 | std::list<std::string> ret; |
|---|
| 30 | int i=0; |
|---|
| 31 | for (unsigned int i=0;i<argLen;i++) { |
|---|
| 32 | std::string s = argument[i]; |
|---|
| 33 | ret.push_back(s); |
|---|
| 34 | } |
|---|
| 35 | return ret; |
|---|
| 36 | } |
|---|
| 37 | /** |
|---|
| 38 | * Translate a message type into a human readable string. |
|---|
| 39 | * |
|---|
| 40 | * @param msgType The message type |
|---|
| 41 | * @return A string representing the message type |
|---|
| 42 | */ |
|---|
| 43 | std::string NSCHelper::translateMessageType(NSCAPI::messageTypes msgType) { |
|---|
| 44 | switch (msgType) { |
|---|
| 45 | case NSCAPI::error: |
|---|
| 46 | return "error"; |
|---|
| 47 | case NSCAPI::critical: |
|---|
| 48 | return "critical"; |
|---|
| 49 | case NSCAPI::warning: |
|---|
| 50 | return "warning"; |
|---|
| 51 | case NSCAPI::log: |
|---|
| 52 | return "message"; |
|---|
| 53 | case NSCAPI::debug: |
|---|
| 54 | return "debug"; |
|---|
| 55 | } |
|---|
| 56 | return "unknown"; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | namespace NSCModuleHelper { |
|---|
| 61 | lpNSAPIGetBasePath fNSAPIGetBasePath = NULL; |
|---|
| 62 | lpNSAPIGetApplicationName fNSAPIGetApplicationName = NULL; |
|---|
| 63 | lpNSAPIGetApplicationVersionStr fNSAPIGetApplicationVersionStr = NULL; |
|---|
| 64 | lpNSAPIGetSettingsString fNSAPIGetSettingsString = NULL; |
|---|
| 65 | lpNSAPIGetSettingsInt fNSAPIGetSettingsInt = NULL; |
|---|
| 66 | lpNSAPIMessage fNSAPIMessage = NULL; |
|---|
| 67 | lpNSAPIStopServer fNSAPIStopServer = NULL; |
|---|
| 68 | lpNSAPIInject fNSAPIInject = NULL; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 72 | // Callbacks into the core |
|---|
| 73 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | * Callback to send a message through to the core |
|---|
| 77 | * |
|---|
| 78 | * @param msgType Message type (debug, warning, etc.) |
|---|
| 79 | * @param file File where message was generated (__FILE__) |
|---|
| 80 | * @param line Line where message was generated (__LINE__) |
|---|
| 81 | * @param message Message in human readable format |
|---|
| 82 | */ |
|---|
| 83 | void NSCModuleHelper::Message(int msgType, std::string file, int line, std::string message) { |
|---|
| 84 | if (!fNSAPIMessage) |
|---|
| 85 | throw "NSCore has not been initiated..."; |
|---|
| 86 | return fNSAPIMessage(msgType, file.c_str(), line, message.c_str()); |
|---|
| 87 | } |
|---|
| 88 | /** |
|---|
| 89 | * Inject a request command in the core (this will then be sent to the plug-in stack for processing) |
|---|
| 90 | * @param command Command to inject (password should not be included. |
|---|
| 91 | * @return The result (if any) of the command. |
|---|
| 92 | */ |
|---|
| 93 | std::string NSCModuleHelper::InjectCommand(std::string command) { |
|---|
| 94 | if (!fNSAPIInject) |
|---|
| 95 | return "NSCore has not been initiated..."; |
|---|
| 96 | char *buffer = new char[BUFF_LEN+1]; |
|---|
| 97 | if (fNSAPIInject(command.c_str(), buffer, BUFF_LEN) != NSCAPI::success) |
|---|
| 98 | throw "Application name could not be retrieved"; |
|---|
| 99 | std::string ret = buffer; |
|---|
| 100 | delete [] buffer; |
|---|
| 101 | return ret; |
|---|
| 102 | } |
|---|
| 103 | /** |
|---|
| 104 | * Ask the core to shutdown (only works when run as a service, o/w does nothing ? |
|---|
| 105 | * @todo Check if this might cause damage if not run as a service. |
|---|
| 106 | */ |
|---|
| 107 | void NSCModuleHelper::StopService(void) { |
|---|
| 108 | if (fNSAPIStopServer) |
|---|
| 109 | fNSAPIStopServer(); |
|---|
| 110 | } |
|---|
| 111 | /** |
|---|
| 112 | * Retrieve a string from the settings subsystem (INI-file) |
|---|
| 113 | * Might possibly be located in the registry in the future. |
|---|
| 114 | * |
|---|
| 115 | * @param section Section key (generally module specific, make sure this is "unique") |
|---|
| 116 | * @param key The key to retrieve |
|---|
| 117 | * @param defaultValue A default value (if no value is set in the settings file) |
|---|
| 118 | * @return the current value or defaultValue if no value is set. |
|---|
| 119 | */ |
|---|
| 120 | std::string NSCModuleHelper::getSettingsString(std::string section, std::string key, std::string defaultValue) { |
|---|
| 121 | if (!fNSAPIGetSettingsString) |
|---|
| 122 | throw "NSCore has not been initiated..."; |
|---|
| 123 | char *buffer = new char[BUFF_LEN+1]; |
|---|
| 124 | int x = fNSAPIGetSettingsString(section.c_str(), key.c_str(), defaultValue.c_str(), buffer, BUFF_LEN); |
|---|
| 125 | std::string ret = buffer; |
|---|
| 126 | delete [] buffer; |
|---|
| 127 | return ret; |
|---|
| 128 | } |
|---|
| 129 | /** |
|---|
| 130 | * Retrieve an int from the settings subsystem (INI-file) |
|---|
| 131 | * Might possibly be located in the registry in the future. |
|---|
| 132 | * |
|---|
| 133 | * @param section Section key (generally module specific, make sure this is "unique") |
|---|
| 134 | * @param key The key to retrieve |
|---|
| 135 | * @param defaultValue A default value (if no value is set in the settings file) |
|---|
| 136 | * @return the current value or defaultValue if no value is set. |
|---|
| 137 | */ |
|---|
| 138 | int NSCModuleHelper::getSettingsInt(std::string section, std::string key, int defaultValue) { |
|---|
| 139 | if (!fNSAPIGetSettingsInt) |
|---|
| 140 | throw "NSCore has not been initiated..."; |
|---|
| 141 | return fNSAPIGetSettingsInt(section.c_str(), key.c_str(), defaultValue); |
|---|
| 142 | } |
|---|
| 143 | /** |
|---|
| 144 | * Retrieve the application name (in human readable format) from the core. |
|---|
| 145 | * @return A string representing the application name. |
|---|
| 146 | */ |
|---|
| 147 | std::string NSCModuleHelper::getApplicationName() { |
|---|
| 148 | if (!fNSAPIGetApplicationName) |
|---|
| 149 | return "NSCore has not been initiated..."; |
|---|
| 150 | char *buffer = new char[BUFF_LEN+1]; |
|---|
| 151 | if (fNSAPIGetApplicationName(buffer, BUFF_LEN) != NSCAPI::success) |
|---|
| 152 | throw "Application name could not be retrieved"; |
|---|
| 153 | std::string ret = buffer; |
|---|
| 154 | delete [] buffer; |
|---|
| 155 | return ret; |
|---|
| 156 | } |
|---|
| 157 | /** |
|---|
| 158 | * Retrieve the directory root of the application from the core. |
|---|
| 159 | * @return A string representing the base path. |
|---|
| 160 | */ |
|---|
| 161 | std::string NSCModuleHelper::getBasePath() { |
|---|
| 162 | if (!fNSAPIGetBasePath) |
|---|
| 163 | return "NSCore has not been initiated..."; |
|---|
| 164 | char *buffer = new char[BUFF_LEN+1]; |
|---|
| 165 | if (fNSAPIGetBasePath(buffer, BUFF_LEN) != NSCAPI::success) |
|---|
| 166 | throw "Base path could not be retrieved"; |
|---|
| 167 | std::string ret = buffer; |
|---|
| 168 | delete [] buffer; |
|---|
| 169 | return ret; |
|---|
| 170 | } |
|---|
| 171 | /** |
|---|
| 172 | * Retrieve the application version as a string (in human readable format) from the core. |
|---|
| 173 | * @return A string representing the application version. |
|---|
| 174 | */ |
|---|
| 175 | std::string NSCModuleHelper::getApplicationVersionString() { |
|---|
| 176 | if (!fNSAPIGetApplicationVersionStr) |
|---|
| 177 | return "NSCore has not been initiated..."; |
|---|
| 178 | char *buffer = new char[BUFF_LEN+1]; |
|---|
| 179 | int x = fNSAPIGetApplicationVersionStr(buffer, BUFF_LEN); |
|---|
| 180 | std::string ret = buffer; |
|---|
| 181 | delete [] buffer; |
|---|
| 182 | return ret; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 186 | // Module helper functions |
|---|
| 187 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 188 | namespace NSCModuleWrapper { |
|---|
| 189 | HINSTANCE hModule_ = NULL; |
|---|
| 190 | } |
|---|
| 191 | /** |
|---|
| 192 | * Used to help store the module handle (and possibly other things in the future) |
|---|
| 193 | * @param hModule cf. DllMain |
|---|
| 194 | * @param ul_reason_for_call cf. DllMain |
|---|
| 195 | * @return TRUE |
|---|
| 196 | */ |
|---|
| 197 | BOOL NSCModuleWrapper::wrapDllMain(HANDLE hModule, DWORD ul_reason_for_call) |
|---|
| 198 | { |
|---|
| 199 | switch (ul_reason_for_call) |
|---|
| 200 | { |
|---|
| 201 | case DLL_PROCESS_ATTACH: |
|---|
| 202 | case DLL_THREAD_ATTACH: |
|---|
| 203 | hModule_ = (HINSTANCE)hModule; |
|---|
| 204 | break; |
|---|
| 205 | case DLL_THREAD_DETACH: |
|---|
| 206 | case DLL_PROCESS_DETACH: |
|---|
| 207 | break; |
|---|
| 208 | } |
|---|
| 209 | return TRUE; |
|---|
| 210 | } |
|---|
| 211 | /** |
|---|
| 212 | * Retrieve the module handle from the DllMain call. |
|---|
| 213 | * @return Module handle of this DLL |
|---|
| 214 | */ |
|---|
| 215 | HINSTANCE NSCModuleWrapper::getModule() { |
|---|
| 216 | return hModule_; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | /** |
|---|
| 220 | * Wrapper function around the ModuleHelperInit call. |
|---|
| 221 | * This wrapper retrieves all pointers and stores them for future use. |
|---|
| 222 | * @param f A function pointer to a function that can be used to load function from the core. |
|---|
| 223 | * @return NSCAPI::success or NSCAPI::failure |
|---|
| 224 | */ |
|---|
| 225 | int NSCModuleWrapper::wrapModuleHelperInit(NSCModuleHelper::lpNSAPILoader f) { |
|---|
| 226 | NSCModuleHelper::fNSAPIGetApplicationName = (NSCModuleHelper::lpNSAPIGetApplicationName)f("NSAPIGetApplicationName"); |
|---|
| 227 | NSCModuleHelper::fNSAPIGetApplicationVersionStr = (NSCModuleHelper::lpNSAPIGetApplicationVersionStr)f("NSAPIGetApplicationVersionStr"); |
|---|
| 228 | NSCModuleHelper::fNSAPIGetSettingsInt = (NSCModuleHelper::lpNSAPIGetSettingsInt)f("NSAPIGetSettingsInt"); |
|---|
| 229 | NSCModuleHelper::fNSAPIGetSettingsString = (NSCModuleHelper::lpNSAPIGetSettingsString)f("NSAPIGetSettingsString"); |
|---|
| 230 | NSCModuleHelper::fNSAPIMessage = (NSCModuleHelper::lpNSAPIMessage)f("NSAPIMessage"); |
|---|
| 231 | NSCModuleHelper::fNSAPIStopServer = (NSCModuleHelper::lpNSAPIStopServer)f("NSAPIStopServer"); |
|---|
| 232 | NSCModuleHelper::fNSAPIInject = (NSCModuleHelper::lpNSAPIInject)f("NSAPIInject"); |
|---|
| 233 | NSCModuleHelper::fNSAPIGetBasePath = (NSCModuleHelper::lpNSAPIGetBasePath)f("NSAPIGetBasePath"); |
|---|
| 234 | return NSCAPI::success; |
|---|
| 235 | } |
|---|
| 236 | /** |
|---|
| 237 | * Wrap the GetModuleName function call |
|---|
| 238 | * @param buf Buffer to store the module name |
|---|
| 239 | * @param bufLen Length of buffer |
|---|
| 240 | * @param str String to store inside the buffer |
|---|
| 241 | * @return buffer copy status |
|---|
| 242 | */ |
|---|
| 243 | int NSCModuleWrapper::wrapGetModuleName(char* buf, unsigned int bufLen, std::string str) { |
|---|
| 244 | return NSCHelper::wrapReturnString(buf, bufLen, str); |
|---|
| 245 | } |
|---|
| 246 | /** |
|---|
| 247 | * Wrap the GetModuleVersion function call |
|---|
| 248 | * @param *major Major version number |
|---|
| 249 | * @param *minor Minor version number |
|---|
| 250 | * @param *revision Revision |
|---|
| 251 | * @param version version as a module_version |
|---|
| 252 | * @return NSCAPI::success |
|---|
| 253 | */ |
|---|
| 254 | int NSCModuleWrapper::wrapGetModuleVersion(int *major, int *minor, int *revision, module_version version) { |
|---|
| 255 | *major = version.major; |
|---|
| 256 | *minor = version.minor; |
|---|
| 257 | *revision = version.revision; |
|---|
| 258 | return NSCAPI::success; |
|---|
| 259 | } |
|---|
| 260 | /** |
|---|
| 261 | * Wrap the HasCommandHandler function call |
|---|
| 262 | * @param has true if this module has a command handler |
|---|
| 263 | * @return NSCAPI::istrue or NSCAPI::isfalse |
|---|
| 264 | */ |
|---|
| 265 | int NSCModuleWrapper::wrapHasCommandHandler(bool has) { |
|---|
| 266 | if (has) |
|---|
| 267 | return NSCAPI::istrue; |
|---|
| 268 | return NSCAPI::isfalse; |
|---|
| 269 | } |
|---|
| 270 | /** |
|---|
| 271 | * Wrap the HasMessageHandler function call |
|---|
| 272 | * @param has true if this module has a message handler |
|---|
| 273 | * @return NSCAPI::istrue or NSCAPI::isfalse |
|---|
| 274 | */ |
|---|
| 275 | int NSCModuleWrapper::wrapHasMessageHandler(bool has) { |
|---|
| 276 | if (has) |
|---|
| 277 | return NSCAPI::istrue; |
|---|
| 278 | return NSCAPI::isfalse; |
|---|
| 279 | } |
|---|
| 280 | /** |
|---|
| 281 | * Wrap the HandleCommand call |
|---|
| 282 | * @param retStr The string to return to the core |
|---|
| 283 | * @param *returnBuffer A buffer to copy the return string to |
|---|
| 284 | * @param returnBufferLen length of returnBuffer |
|---|
| 285 | * @return copy status or NSCAPI::isfalse if retStr is empty |
|---|
| 286 | */ |
|---|
| 287 | int NSCModuleWrapper::wrapHandleCommand(const std::string retStr, char *returnBuffer, unsigned int returnBufferLen) { |
|---|
| 288 | if (retStr.empty()) |
|---|
| 289 | return NSCAPI::isfalse; |
|---|
| 290 | return NSCHelper::wrapReturnString(returnBuffer, returnBufferLen, retStr, NSCAPI::handled); |
|---|
| 291 | } |
|---|
| 292 | /** |
|---|
| 293 | * Wrap the NSLoadModule call |
|---|
| 294 | * @param success true if module load was successfully |
|---|
| 295 | * @return NSCAPI::success or NSCAPI::failed |
|---|
| 296 | */ |
|---|
| 297 | int NSCModuleWrapper::wrapLoadModule(bool success) { |
|---|
| 298 | if (success) |
|---|
| 299 | return NSCAPI::success; |
|---|
| 300 | return NSCAPI::failed; |
|---|
| 301 | } |
|---|
| 302 | /** |
|---|
| 303 | * Wrap the NSUnloadModule call |
|---|
| 304 | * @param success true if module load was successfully |
|---|
| 305 | * @return NSCAPI::success or NSCAPI::failed |
|---|
| 306 | */ |
|---|
| 307 | int NSCModuleWrapper::wrapUnloadModule(bool success) { |
|---|
| 308 | if (success) |
|---|
| 309 | return NSCAPI::success; |
|---|
| 310 | return NSCAPI::failed; |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | |
|---|