"Fossies" - the Fresh Open Source Software Archive

Member "netxms-3.8.166/src/agent/libnxagent/bridge.cpp" (23 Feb 2021, 9534 Bytes) of package /linux/misc/netxms-3.8.166.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "bridge.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 3.7.145_vs_3.8.120.

    1 /*
    2 ** NetXMS - Network Management System
    3 ** Copyright (C) 2003-2021 Victor Kirhenshtein
    4 **
    5 ** This program is free software; you can redistribute it and/or modify
    6 ** it under the terms of the GNU Lesser General Public License as published
    7 ** by the Free Software Foundation; either version 3 of the License, or
    8 ** (at your option) any later version.
    9 **
   10 ** This program is distributed in the hope that it will be useful,
   11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13 ** GNU General Public License for more details.
   14 **
   15 ** You should have received a copy of the GNU Lesser General Public License
   16 ** along with this program; if not, write to the Free Software
   17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   18 **
   19 ** File: bridge.cpp
   20 **
   21 **/
   22 
   23 #include "libnxagent.h"
   24 
   25 /**
   26  * Static data
   27  */
   28 static void (* s_fpWriteLog)(int, int, const TCHAR *) = nullptr;
   29 static void (* s_fpPostEvent1)(uint32_t, const TCHAR *, time_t, const char *, va_list) = nullptr;
   30 static void (* s_fpPostEvent2)(uint32_t, const TCHAR *, time_t, int, const TCHAR **) = nullptr;
   31 static shared_ptr<AbstractCommSession> (* s_fpFindServerSession)(uint64_t) = nullptr;
   32 static bool (* s_fpEnumerateSessions)(EnumerationCallbackResult (*)(AbstractCommSession *, void *), void *) = nullptr;
   33 static bool (* s_fpSendFile)(void *, UINT32, const TCHAR *, long, bool, VolatileCounter *) = nullptr;
   34 static bool (* s_fpPushData)(const TCHAR *, const TCHAR *, UINT32, time_t) = nullptr;
   35 static const TCHAR *s_dataDirectory = nullptr;
   36 static DB_HANDLE (*s_fpGetLocalDatabaseHandle)() = nullptr;
   37 static void (*s_fpExecuteAction)(const TCHAR*, const StringList&) = nullptr;
   38 static bool (*s_fpGetScreenInfoForUserSession)(uint32_t, uint32_t *, uint32_t *, uint32_t *) = nullptr;
   39 static void (*s_fpQueueNotificationMessage)(NXCPMessage *) = nullptr;
   40 
   41 /**
   42  * Initialize subagent API
   43  */
   44 void LIBNXAGENT_EXPORTABLE InitSubAgentAPI(
   45       void (*writeLog)(int, int, const TCHAR *),
   46       void (*postEvent1)(uint32_t, const TCHAR *, time_t, const char *, va_list),
   47       void (*postEvent2)(uint32_t, const TCHAR *, time_t, int, const TCHAR **),
   48       bool (*enumerateSessions)(EnumerationCallbackResult (*)(AbstractCommSession *, void *), void*),
   49       shared_ptr<AbstractCommSession> (*findServerSession)(uint64_t),
   50       bool (*sendFile)(void *, UINT32, const TCHAR *, long, bool, VolatileCounter *),
   51       bool (*pushData)(const TCHAR *, const TCHAR *, UINT32, time_t),
   52       DB_HANDLE (*getLocalDatabaseHandle)(),
   53       const TCHAR *dataDirectory,
   54       void (*executeAction)(const TCHAR*, const StringList&),
   55       bool (*getScreenInfoForUserSession)(uint32_t, uint32_t *, uint32_t *, uint32_t *),
   56       void (*queueNotificationMessage)(NXCPMessage*))
   57 {
   58    s_fpWriteLog = writeLog;
   59     s_fpPostEvent1 = postEvent1;
   60     s_fpPostEvent2 = postEvent2;
   61     s_fpEnumerateSessions = enumerateSessions;
   62    s_fpFindServerSession = findServerSession;
   63     s_fpSendFile = sendFile;
   64     s_fpPushData = pushData;
   65    s_dataDirectory = dataDirectory;
   66    s_fpGetLocalDatabaseHandle = getLocalDatabaseHandle;
   67    s_fpExecuteAction = executeAction;
   68    s_fpGetScreenInfoForUserSession = getScreenInfoForUserSession;
   69    s_fpQueueNotificationMessage = queueNotificationMessage;
   70 }
   71 
   72 /**
   73  * Write message to agent's log
   74  */
   75 void LIBNXAGENT_EXPORTABLE AgentWriteLog(int logLevel, const TCHAR *format, ...)
   76 {
   77    TCHAR szBuffer[4096];
   78    va_list args;
   79 
   80    if (s_fpWriteLog != nullptr)
   81    {
   82       va_start(args, format);
   83       _vsntprintf(szBuffer, 4096, format, args);
   84       va_end(args);
   85       szBuffer[4095] = 0;
   86       s_fpWriteLog(logLevel, 0, szBuffer);
   87    }
   88 }
   89 
   90 /**
   91  * Write message to agent's log
   92  */
   93 void LIBNXAGENT_EXPORTABLE AgentWriteLog2(int logLevel, const TCHAR *format, va_list args)
   94 {
   95    TCHAR szBuffer[4096];
   96 
   97    if (s_fpWriteLog != nullptr)
   98    {
   99       _vsntprintf(szBuffer, 4096, format, args);
  100       szBuffer[4095] = 0;
  101       s_fpWriteLog(logLevel, 0, szBuffer);
  102    }
  103 }
  104 
  105 /**
  106  * Write debug message to agent's log
  107  */
  108 void LIBNXAGENT_EXPORTABLE AgentWriteDebugLog(int level, const TCHAR *format, ...)
  109 {
  110    TCHAR szBuffer[4096];
  111    va_list args;
  112 
  113    if (s_fpWriteLog != nullptr)
  114    {
  115       va_start(args, format);
  116       _vsntprintf(szBuffer, 4096, format, args);
  117       va_end(args);
  118       szBuffer[4095] = 0;
  119       s_fpWriteLog(EVENTLOG_DEBUG_TYPE, level, szBuffer);
  120    }
  121 }
  122 
  123 /**
  124  * Write debug message to agent's log
  125  */
  126 void LIBNXAGENT_EXPORTABLE AgentWriteDebugLog2(int level, const TCHAR *format, va_list args)
  127 {
  128    TCHAR szBuffer[4096];
  129 
  130    if (s_fpWriteLog != nullptr)
  131    {
  132       _vsntprintf(szBuffer, 4096, format, args);
  133       szBuffer[4095] = 0;
  134       s_fpWriteLog(EVENTLOG_DEBUG_TYPE, level, szBuffer);
  135    }
  136 }
  137 
  138 /**
  139  * Send event from agent to server
  140  */
  141 void LIBNXAGENT_EXPORTABLE AgentPostEvent(uint32_t eventCode, const TCHAR *eventName, time_t timestamp, const char *format, ...)
  142 {
  143    if (s_fpPostEvent1 != nullptr)
  144    {
  145       va_list args;
  146       va_start(args, format);
  147       s_fpPostEvent1(eventCode, eventName, timestamp, format, args);
  148       va_end(args);
  149    }
  150 }
  151 
  152 /**
  153  * Send event from agent to server
  154  */
  155 void LIBNXAGENT_EXPORTABLE AgentPostEvent2(uint32_t eventCode, const TCHAR *eventName, time_t timestamp, int count, const TCHAR **args)
  156 {
  157    if (s_fpPostEvent2 != nullptr)
  158       s_fpPostEvent2(eventCode, eventName, timestamp, count, args);
  159 }
  160 
  161 /**
  162  * Enumerates active agent sessions. Callback will be called for each valid session.
  163  * Callback must return _STOP to stop enumeration or _CONTINUE to continue.
  164  *
  165  * @return true if enumeration was stopped by callback
  166  */
  167 bool LIBNXAGENT_EXPORTABLE AgentEnumerateSessions(EnumerationCallbackResult (* callback)(AbstractCommSession *, void *), void *data)
  168 {
  169    return (s_fpEnumerateSessions != nullptr) ? s_fpEnumerateSessions(callback, data) : false;
  170 }
  171 
  172 /**
  173  * Send file to server
  174  */
  175 bool LIBNXAGENT_EXPORTABLE AgentSendFileToServer(void *session, UINT32 requestId, const TCHAR *file, long offset, 
  176                                                  bool allowCompression, VolatileCounter *cancellationFlag)
  177 {
  178     if ((s_fpSendFile == nullptr) || (session == nullptr) || (file == nullptr))
  179         return FALSE;
  180     return s_fpSendFile(session, requestId, file, offset, allowCompression, cancellationFlag);
  181 }
  182 
  183 /**
  184  * Push parameter's value
  185  */
  186 bool LIBNXAGENT_EXPORTABLE AgentPushParameterData(const TCHAR *parameter, const TCHAR *value)
  187 {
  188     if (s_fpPushData == nullptr)
  189         return FALSE;
  190     return s_fpPushData(parameter, value, 0, 0);
  191 }
  192 
  193 /**
  194  * Push parameter's value
  195  */
  196 bool LIBNXAGENT_EXPORTABLE AgentPushParameterDataInt32(const TCHAR *parameter, LONG value)
  197 {
  198     TCHAR buffer[64];
  199 
  200     _sntprintf(buffer, sizeof(buffer), _T("%d"), (int)value);
  201     return AgentPushParameterData(parameter, buffer);
  202 }
  203 
  204 /**
  205  * Push parameter's value
  206  */
  207 bool LIBNXAGENT_EXPORTABLE AgentPushParameterDataUInt32(const TCHAR *parameter, UINT32 value)
  208 {
  209     TCHAR buffer[64];
  210 
  211     _sntprintf(buffer, sizeof(buffer), _T("%u"), (unsigned int)value);
  212     return AgentPushParameterData(parameter, buffer);
  213 }
  214 
  215 /**
  216  * Push parameter's value
  217  */
  218 bool LIBNXAGENT_EXPORTABLE AgentPushParameterDataInt64(const TCHAR *parameter, INT64 value)
  219 {
  220     TCHAR buffer[64];
  221 
  222     _sntprintf(buffer, sizeof(buffer), INT64_FMT, value);
  223     return AgentPushParameterData(parameter, buffer);
  224 }
  225 
  226 /**
  227  * Push parameter's value
  228  */
  229 bool LIBNXAGENT_EXPORTABLE AgentPushParameterDataUInt64(const TCHAR *parameter, QWORD value)
  230 {
  231     TCHAR buffer[64];
  232 
  233     _sntprintf(buffer, sizeof(buffer), UINT64_FMT, value);
  234     return AgentPushParameterData(parameter, buffer);
  235 }
  236 
  237 /**
  238  * Push parameter's value
  239  */
  240 bool LIBNXAGENT_EXPORTABLE AgentPushParameterDataDouble(const TCHAR *parameter, double value)
  241 {
  242     TCHAR buffer[64];
  243 
  244     _sntprintf(buffer, sizeof(buffer), _T("%f"), value);
  245     return AgentPushParameterData(parameter, buffer);
  246 }
  247 
  248 /**
  249  * Get data directory
  250  */
  251 const TCHAR LIBNXAGENT_EXPORTABLE *AgentGetDataDirectory()
  252 {
  253    return s_dataDirectory;
  254 }
  255 
  256 /**
  257  * Find server session. Caller must call decRefCount() for session object when finished.
  258  *
  259  * @param serverId server ID
  260  * @return server session object or nullptr
  261  */
  262 shared_ptr<AbstractCommSession> LIBNXAGENT_EXPORTABLE AgentFindServerSession(uint64_t serverId)
  263 {
  264    return (s_fpFindServerSession != nullptr) ? s_fpFindServerSession(serverId) : shared_ptr<AbstractCommSession>();
  265 }
  266 
  267 /**
  268  * Get handle to local database.
  269  *
  270  * @return database handle or nullptr if not available
  271  */
  272 DB_HANDLE LIBNXAGENT_EXPORTABLE AgentGetLocalDatabaseHandle()
  273 {
  274    return (s_fpGetLocalDatabaseHandle != nullptr) ? s_fpGetLocalDatabaseHandle() : nullptr;
  275 }
  276 
  277 /**
  278  * Execute agent action or command line command
  279  *
  280  * @param agent action or command
  281  */
  282 void LIBNXAGENT_EXPORTABLE AgentExecuteAction(const TCHAR *action, const StringList& args)
  283 {
  284    if (s_fpExecuteAction != nullptr)
  285       s_fpExecuteAction(action, args);
  286 }
  287 
  288 /**
  289  * Get screen information for given user session via session agent.
  290  */
  291 bool LIBNXAGENT_EXPORTABLE AgentGetScreenInfoForUserSession(uint32_t sessionId, uint32_t *width, uint32_t *height, uint32_t *bpp)
  292 {
  293    return (s_fpGetScreenInfoForUserSession != nullptr) ? s_fpGetScreenInfoForUserSession(sessionId, width, height, bpp) : false;
  294 }
  295 
  296 /**
  297  * Add message to notification queue. Ownership will be taken by the queue and
  298  * pointer to message should be considered invalid after this call.
  299  */
  300 void LIBNXAGENT_EXPORTABLE AgentQueueNotifictionMessage(NXCPMessage *msg)
  301 {
  302    if (s_fpQueueNotificationMessage != nullptr)
  303       s_fpQueueNotificationMessage(msg);
  304    else
  305       delete msg;
  306 }