license for libnxdb and libnxsrv changed to LGPL
[public/netxms.git] / src / server / libnxsrv / main.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Server Library
4 ** Copyright (C) 2003-2010 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU Lesser General Public License as published by
8 ** the Free Software Foundation; either version 3 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 Lesser 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 ** File: main.cpp
21 **
22 **/
23
24 #include "libnxsrv.h"
25
26
27 //
28 // Global variables
29 //
30
31 DWORD LIBNXSRV_EXPORTABLE g_dwFlags = AF_USE_SYSLOG;
32 int LIBNXSRV_EXPORTABLE g_nDebugLevel = 0;
33
34
35 //
36 // Agent result codes
37 //
38
39 static struct
40 {
41 int iCode;
42 const TCHAR *pszText;
43 } m_agentErrors[] =
44 {
45 { ERR_SUCCESS, _T("Success") },
46 { ERR_UNKNOWN_COMMAND, _T("Unknown command") },
47 { ERR_AUTH_REQUIRED, _T("Authentication required") },
48 { ERR_ACCESS_DENIED, _T("Access denied") },
49 { ERR_UNKNOWN_PARAMETER, _T("Unknown parameter") },
50 { ERR_REQUEST_TIMEOUT, _T("Request timeout") },
51 { ERR_AUTH_FAILED, _T("Authentication failed") },
52 { ERR_ALREADY_AUTHENTICATED, _T("Already authenticated") },
53 { ERR_AUTH_NOT_REQUIRED, _T("Authentication not required") },
54 { ERR_INTERNAL_ERROR, _T("Internal error") },
55 { ERR_NOT_IMPLEMENTED, _T("Not implemented") },
56 { ERR_OUT_OF_RESOURCES, _T("Out of resources") },
57 { ERR_NOT_CONNECTED, _T("Not connected") },
58 { ERR_CONNECTION_BROKEN, _T("Connection broken") },
59 { ERR_BAD_RESPONSE, _T("Bad response") },
60 { ERR_IO_FAILURE, _T("I/O failure") },
61 { ERR_RESOURCE_BUSY, _T("Resource busy") },
62 { ERR_EXEC_FAILED, _T("External program execution failed") },
63 { ERR_ENCRYPTION_REQUIRED, _T("Encryption required") },
64 { ERR_NO_CIPHERS, _T("No acceptable ciphers") },
65 { ERR_INVALID_PUBLIC_KEY, _T("Invalid public key") },
66 { ERR_INVALID_SESSION_KEY, _T("Invalid session key") },
67 { ERR_CONNECT_FAILED, _T("Connect failed") },
68 { ERR_MALFORMED_COMMAND, _T("Malformed command") },
69 { ERR_SOCKET_ERROR, _T("Socket error") },
70 { ERR_BAD_ARGUMENTS, _T("Bad arguments") },
71 { ERR_SUBAGENT_LOAD_FAILED, _T("Subagent load failed") },
72 { ERR_FILE_OPEN_ERROR, _T("File open error") },
73 { ERR_FILE_STAT_FAILED, _T("File stat filed") },
74 { ERR_MEM_ALLOC_FAILED, _T("Memory allocation failed") },
75 { ERR_FILE_DELETE_FAILED, _T("File delete failed") },
76 { -1, NULL }
77 };
78
79
80 //
81 // Resolve agent's error code to text
82 //
83
84 const TCHAR LIBNXSRV_EXPORTABLE *AgentErrorCodeToText(int iError)
85 {
86 int i;
87
88 for(i = 0; m_agentErrors[i].pszText != NULL; i++)
89 if (iError == m_agentErrors[i].iCode)
90 return m_agentErrors[i].pszText;
91 return _T("Unknown error code");
92 }
93
94
95 //
96 // Destroy interface list created by discovery functions
97 //
98
99 void LIBNXSRV_EXPORTABLE DestroyInterfaceList(INTERFACE_LIST *pIfList)
100 {
101 if (pIfList != NULL)
102 {
103 if (pIfList->pInterfaces != NULL)
104 free(pIfList->pInterfaces);
105 free(pIfList);
106 }
107 }
108
109
110 //
111 // Destroy ARP cache created by discovery functions
112 //
113
114 void LIBNXSRV_EXPORTABLE DestroyArpCache(ARP_CACHE *pArpCache)
115 {
116 if (pArpCache != NULL)
117 {
118 if (pArpCache->pEntries != NULL)
119 free(pArpCache->pEntries);
120 free(pArpCache);
121 }
122 }
123
124
125 //
126 // Destroy routing table
127 //
128
129 void LIBNXSRV_EXPORTABLE DestroyRoutingTable(ROUTING_TABLE *pRT)
130 {
131 if (pRT != NULL)
132 {
133 safe_free(pRT->pRoutes);
134 free(pRT);
135 }
136 }
137
138
139 //
140 // Route comparision callback
141 //
142
143 static int CompareRoutes(const void *p1, const void *p2)
144 {
145 return -(COMPARE_NUMBERS(((ROUTE *)p1)->dwDestMask, ((ROUTE *)p2)->dwDestMask));
146 }
147
148
149 //
150 // Sort routing table (put most specific routes first)
151 //
152
153 void LIBNXSRV_EXPORTABLE SortRoutingTable(ROUTING_TABLE *pRT)
154 {
155 qsort(pRT->pRoutes, pRT->iNumEntries, sizeof(ROUTE), CompareRoutes);
156 }
157
158
159 //
160 // Debug printf - write debug record to log if level is less or equal current debug level
161 //
162
163 void LIBNXSRV_EXPORTABLE DbgPrintf2(int level, const TCHAR *format, va_list args)
164 {
165 TCHAR buffer[4096];
166
167 if (level > g_nDebugLevel)
168 return; // Required application flag(s) not set
169
170 _vsntprintf(buffer, 4096, format, args);
171 nxlog_write(MSG_DEBUG, EVENTLOG_DEBUG_TYPE, "s", buffer);
172 }
173
174 void LIBNXSRV_EXPORTABLE DbgPrintf(int level, const TCHAR *format, ...)
175 {
176 va_list args;
177 TCHAR buffer[4096];
178
179 if (level > g_nDebugLevel)
180 return; // Required application flag(s) not set
181
182 va_start(args, format);
183 _vsntprintf(buffer, 4096, format, args);
184 va_end(args);
185 nxlog_write(MSG_DEBUG, EVENTLOG_DEBUG_TYPE, "s", buffer);
186 }
187
188
189 //
190 // Log custom message (mostly used by modules)
191 //
192
193 void LIBNXSRV_EXPORTABLE WriteLogOther(WORD wType, const TCHAR *format, ...)
194 {
195 va_list args;
196 TCHAR buffer[4096];
197
198 va_start(args, format);
199 _vsntprintf(buffer, 4096, format, args);
200 va_end(args);
201 nxlog_write(MSG_OTHER, wType, "s", buffer);
202 }
203
204
205 //
206 // DLL entry point
207 //
208
209 #ifdef _WIN32
210
211 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
212 {
213 if (dwReason == DLL_PROCESS_ATTACH)
214 DisableThreadLibraryCalls(hInstance);
215 return TRUE;
216 }
217
218 #endif /* _WIN32 */