aba19460e58077f34b23f5594b7b13dc95e85dd0
[public/netxms.git] / src / server / include / nxsrvapi.h
1 /*
2 ** NetXMS - Network Management System
3 ** Server Library
4 ** Copyright (C) 2003, 2004 Victor Kirhenshtein
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 Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** $module: nxsrvapi.h
21 **
22 **/
23
24 #ifndef _nxsrvapi_h_
25 #define _nxsrvapi_h_
26
27 #ifdef _WIN32
28 #ifdef LIBNXSRV_EXPORTS
29 #define LIBNXSRV_EXPORTABLE __declspec(dllexport)
30 #else
31 #define LIBNXSRV_EXPORTABLE __declspec(dllimport)
32 #endif
33 #else /* _WIN32 */
34 #define LIBNXSRV_EXPORTABLE
35 #endif
36
37 #ifndef LIBNXCL_NO_DECLARATIONS
38 #define LIBNXCL_NO_DECLARATIONS 1
39 #endif
40 #include <nxclapi.h>
41 #include <nxcscpapi.h>
42 #include <nms_agent.h>
43 #include <rwlock.h>
44 #include <messages.h>
45 #include <dbdrv.h>
46
47
48 //
49 // Default files
50 //
51
52 #ifdef _WIN32
53 #define DEFAULT_CONFIG_FILE _T("C:\\NetXMS.conf")
54 #else
55 #define DEFAULT_CONFIG_FILE _T("/etc/netxmsd.conf")
56 #endif
57
58
59 //
60 // DB-related constants
61 //
62
63 #define MAX_DB_LOGIN 64
64 #define MAX_DB_PASSWORD 64
65 #define MAX_DB_NAME 32
66
67
68 //
69 // Win32 service constants
70 //
71
72 #ifdef _WIN32
73
74 #define CORE_SERVICE_NAME _T("NetXMSCore")
75 #define CORE_EVENT_SOURCE _T("NetXMSCore")
76
77 #endif /* _WIN32 */
78
79
80 //
81 // Single ARP cache entry
82 //
83
84 typedef struct
85 {
86 DWORD dwIndex; // Interface index
87 DWORD dwIpAddr;
88 BYTE bMacAddr[MAC_ADDR_LENGTH];
89 } ARP_ENTRY;
90
91
92 //
93 // ARP cache structure used by discovery functions and AgentConnection class
94 //
95
96 typedef struct
97 {
98 DWORD dwNumEntries;
99 ARP_ENTRY *pEntries;
100 } ARP_CACHE;
101
102
103 //
104 // Interface information structure used by discovery functions and AgentConnection class
105 //
106
107 typedef struct
108 {
109 TCHAR szName[MAX_OBJECT_NAME];
110 DWORD dwIndex;
111 DWORD dwType;
112 DWORD dwIpAddr;
113 DWORD dwIpNetMask;
114 BYTE bMacAddr[MAC_ADDR_LENGTH];
115 int iNumSecondary; // Number of secondary IP's on this interface
116 } INTERFACE_INFO;
117
118
119 //
120 // Interface list used by discovery functions and AgentConnection class
121 //
122
123 typedef struct
124 {
125 int iNumEntries; // Number of entries in pInterfaces
126 int iEnumPos; // Used by index enumeration handler
127 void *pArg; // Can be used by custom enumeration handlers
128 INTERFACE_INFO *pInterfaces; // Interface entries
129 } INTERFACE_LIST;
130
131
132 //
133 // Agent connection
134 //
135
136 class LIBNXSRV_EXPORTABLE AgentConnection
137 {
138 private:
139 DWORD m_dwAddr;
140 int m_iAuthMethod;
141 char m_szSecret[MAX_SECRET_LENGTH];
142 time_t m_tLastCommandTime;
143 SOCKET m_hSocket;
144 WORD m_wPort;
145 DWORD m_dwNumDataLines;
146 DWORD m_dwRequestId;
147 DWORD m_dwCommandTimeout;
148 TCHAR **m_ppDataLines;
149 MsgWaitQueue *m_pMsgWaitQueue;
150 BOOL m_bIsConnected;
151 MUTEX m_mutexDataLock;
152 THREAD m_hReceiverThread;
153
154 void ReceiverThread(void);
155 static THREAD_RESULT THREAD_CALL ReceiverThreadStarter(void *);
156
157 protected:
158 void DestroyResultData(void);
159 BOOL SendMessage(CSCPMessage *pMsg);
160 CSCPMessage *WaitForMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut) { return m_pMsgWaitQueue->WaitForMessage(wCode, dwId, dwTimeOut); }
161 DWORD WaitForRCC(DWORD dwRqId, DWORD dwTimeOut);
162 DWORD Authenticate(void);
163
164 virtual void PrintMsg(TCHAR *pszFormat, ...);
165 virtual void OnTrap(CSCPMessage *pMsg);
166
167 void Lock(void) { MutexLock(m_mutexDataLock, INFINITE); }
168 void Unlock(void) { MutexUnlock(m_mutexDataLock); }
169
170 public:
171 AgentConnection();
172 AgentConnection(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT, int iAuthMethod = AUTH_NONE, TCHAR *szSecret = NULL);
173 ~AgentConnection();
174
175 BOOL Connect(BOOL bVerbose = FALSE);
176 void Disconnect(void);
177 BOOL IsConnected(void) { return m_bIsConnected; }
178
179 ARP_CACHE *GetArpCache(void);
180 INTERFACE_LIST *GetInterfaceList(void);
181 DWORD GetParameter(TCHAR *pszParam, DWORD dwBufSize, TCHAR *pszBuffer);
182 DWORD GetList(TCHAR *pszParam);
183 DWORD Nop(void);
184 DWORD ExecAction(TCHAR *pszAction, int argc, TCHAR **argv);
185 DWORD UploadFile(TCHAR *pszFile);
186 DWORD StartUpgrade(TCHAR *pszPkgName);
187
188 DWORD GetNumDataLines(void) { return m_dwNumDataLines; }
189 const TCHAR *GetDataLine(DWORD dwIndex) { return dwIndex < m_dwNumDataLines ? m_ppDataLines[dwIndex] : _T("(error)"); }
190
191 void SetCommandTimeout(DWORD dwTimeout) { if (dwTimeout > 500) m_dwCommandTimeout = dwTimeout; }
192 };
193
194
195 //
196 // Functions
197 //
198
199 void LIBNXSRV_EXPORTABLE DestroyArpCache(ARP_CACHE *pArpCache);
200 void LIBNXSRV_EXPORTABLE DestroyInterfaceList(INTERFACE_LIST *pIfList);
201 const TCHAR LIBNXSRV_EXPORTABLE *AgentErrorCodeToText(int iError);
202
203 void LIBNXSRV_EXPORTABLE InitLog(BOOL bUseSystemLog, char *pszLogFile, BOOL bPrintToScreen);
204 void LIBNXSRV_EXPORTABLE CloseLog(void);
205 void LIBNXSRV_EXPORTABLE WriteLog(DWORD msg, WORD wType, char *format, ...);
206
207 BOOL LIBNXSRV_EXPORTABLE DBInit(BOOL bWriteLog, BOOL bLogErrors);
208 DB_HANDLE LIBNXSRV_EXPORTABLE DBConnect(void);
209 void LIBNXSRV_EXPORTABLE DBDisconnect(DB_HANDLE hConn);
210 BOOL LIBNXSRV_EXPORTABLE DBQuery(DB_HANDLE hConn, char *szQuery);
211 DB_RESULT LIBNXSRV_EXPORTABLE DBSelect(DB_HANDLE hConn, char *szQuery);
212 DB_ASYNC_RESULT LIBNXSRV_EXPORTABLE DBAsyncSelect(DB_HANDLE hConn, char *szQuery);
213 BOOL LIBNXSRV_EXPORTABLE DBFetch(DB_ASYNC_RESULT hResult);
214 char LIBNXSRV_EXPORTABLE *DBGetField(DB_RESULT hResult, int iRow, int iColumn);
215 long LIBNXSRV_EXPORTABLE DBGetFieldLong(DB_RESULT hResult, int iRow, int iColumn);
216 DWORD LIBNXSRV_EXPORTABLE DBGetFieldULong(DB_RESULT hResult, int iRow, int iColumn);
217 INT64 LIBNXSRV_EXPORTABLE DBGetFieldInt64(DB_RESULT hResult, int iRow, int iColumn);
218 QWORD LIBNXSRV_EXPORTABLE DBGetFieldUInt64(DB_RESULT hResult, int iRow, int iColumn);
219 double LIBNXSRV_EXPORTABLE DBGetFieldDouble(DB_RESULT hResult, int iRow, int iColumn);
220 DWORD LIBNXSRV_EXPORTABLE DBGetFieldIPAddr(DB_RESULT hResult, int iRow, int iColumn);
221 char LIBNXSRV_EXPORTABLE *DBGetFieldAsync(DB_ASYNC_RESULT hResult, int iColumn, char *pBuffer, int iBufSize);
222 long LIBNXSRV_EXPORTABLE DBGetFieldAsyncLong(DB_RESULT hResult, int iColumn);
223 DWORD LIBNXSRV_EXPORTABLE DBGetFieldAsyncULong(DB_ASYNC_RESULT hResult, int iColumn);
224 INT64 LIBNXSRV_EXPORTABLE DBGetFieldAsyncInt64(DB_RESULT hResult, int iColumn);
225 QWORD LIBNXSRV_EXPORTABLE DBGetFieldAsyncUInt64(DB_ASYNC_RESULT hResult, int iColumn);
226 double LIBNXSRV_EXPORTABLE DBGetFieldAsyncDouble(DB_RESULT hResult, int iColumn);
227 DWORD LIBNXSRV_EXPORTABLE DBGetFieldAsyncIPAddr(DB_RESULT hResult, int iColumn);
228 int LIBNXSRV_EXPORTABLE DBGetNumRows(DB_RESULT hResult);
229 void LIBNXSRV_EXPORTABLE DBFreeResult(DB_RESULT hResult);
230 void LIBNXSRV_EXPORTABLE DBFreeAsyncResult(DB_ASYNC_RESULT hResult);
231 void LIBNXSRV_EXPORTABLE DBUnloadDriver(void);
232
233
234 //
235 // Variables
236 //
237
238 extern char LIBNXSRV_EXPORTABLE g_szDbDriver[];
239 extern char LIBNXSRV_EXPORTABLE g_szDbDriver[];
240 extern char LIBNXSRV_EXPORTABLE g_szDbDrvParams[];
241 extern char LIBNXSRV_EXPORTABLE g_szDbServer[];
242 extern char LIBNXSRV_EXPORTABLE g_szDbLogin[];
243 extern char LIBNXSRV_EXPORTABLE g_szDbPassword[];
244 extern char LIBNXSRV_EXPORTABLE g_szDbName[];
245
246 #endif /* _nxsrvapi_h_ */