initial work on network device drivers
[public/netxms.git] / src / server / core / node.cpp
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
6e388680 3** Copyright (C) 2003-2011 Victor Kirhenshtein
5039dede
AK
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 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 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: node.cpp
20**
21**/
22
23#include "nxcore.h"
24
25
26//
5039dede
AK
27// Node class default constructor
28//
29
30Node::Node()
31 :Template()
32{
a65c1819 33 m_iStatus = STATUS_UNKNOWN;
5039dede
AK
34 m_dwFlags = 0;
35 m_dwDynamicFlags = 0;
36 m_dwZoneGUID = 0;
37 m_dwNodeType = NODE_TYPE_GENERIC;
38 m_wAgentPort = AGENT_LISTEN_PORT;
39 m_wAuthMethod = AUTH_NONE;
40 m_szSharedSecret[0] = 0;
41 m_iStatusPollType = POLL_ICMP_PING;
5d2c5741 42 m_snmpVersion = SNMP_VERSION_1;
5039dede 43 m_wSNMPPort = SNMP_DEFAULT_PORT;
35f836fe
VK
44 char community[MAX_COMMUNITY_LENGTH];
45 ConfigReadStrA(_T("DefaultCommunityString"), community, MAX_COMMUNITY_LENGTH, "public");
5d2c5741 46 m_snmpSecurity = new SNMP_SecurityContext(community);
5039dede
AK
47 m_szObjectId[0] = 0;
48 m_tLastDiscoveryPoll = 0;
49 m_tLastStatusPoll = 0;
50 m_tLastConfigurationPoll = 0;
51 m_tLastRTUpdate = 0;
52 m_hPollerMutex = MutexCreate();
53 m_hAgentAccessMutex = MutexCreate();
54 m_mutexRTAccess = MutexCreate();
55 m_mutexTopoAccess = MutexCreate();
56 m_pAgentConnection = NULL;
57 m_szAgentVersion[0] = 0;
58 m_szPlatformName[0] = 0;
0ecc2200
VK
59 m_sysDescription = NULL;
60 m_sysName = NULL;
61 m_lldpNodeId = NULL;
5039dede
AK
62 m_dwNumParams = 0;
63 m_pParamList = NULL;
64 m_dwPollerNode = 0;
65 m_dwProxyNode = 0;
66 m_dwSNMPProxy = 0;
67 memset(m_qwLastEvents, 0, sizeof(QWORD) * MAX_LAST_EVENTS);
68 m_pRoutingTable = NULL;
69 m_tFailTimeSNMP = 0;
70 m_tFailTimeAgent = 0;
040c45fa 71 m_linkLayerNeighbors = NULL;
0d75ea88 72 m_vrrpInfo = NULL;
5039dede 73 m_tLastTopologyPoll = 0;
040c45fa
VK
74 m_pTopology = NULL;
75 m_topologyRebuildTimestamp = 0;
5039dede
AK
76 m_iPendingStatus = -1;
77 m_iPollCount = 0;
78 m_iRequiredPollCount = 0; // Use system default
79 m_nUseIfXTable = IFXTABLE_DEFAULT; // Use system default
ab621f39 80 m_jobQueue = new ServerJobQueue();
f42b8099 81 m_fdb = NULL;
5039dede
AK
82}
83
84
85//
86// Constructor for new node object
87//
88
89Node::Node(DWORD dwAddr, DWORD dwFlags, DWORD dwProxyNode, DWORD dwSNMPProxy, DWORD dwZone)
90 :Template()
91{
a65c1819 92 m_iStatus = STATUS_UNKNOWN;
5039dede
AK
93 m_dwIpAddr = dwAddr;
94 m_dwFlags = dwFlags;
95 m_dwDynamicFlags = 0;
96 m_dwZoneGUID = dwZone;
97 m_dwNodeType = NODE_TYPE_GENERIC;
98 m_wAgentPort = AGENT_LISTEN_PORT;
99 m_wAuthMethod = AUTH_NONE;
100 m_szSharedSecret[0] = 0;
101 m_iStatusPollType = POLL_ICMP_PING;
5d2c5741 102 m_snmpVersion = SNMP_VERSION_1;
5039dede 103 m_wSNMPPort = SNMP_DEFAULT_PORT;
35f836fe
VK
104 char community[MAX_COMMUNITY_LENGTH];
105 ConfigReadStrA(_T("DefaultCommunityString"), community, MAX_COMMUNITY_LENGTH, "public");
5d2c5741 106 m_snmpSecurity = new SNMP_SecurityContext(community);
5039dede
AK
107 IpToStr(dwAddr, m_szName); // Make default name from IP address
108 m_szObjectId[0] = 0;
109 m_tLastDiscoveryPoll = 0;
110 m_tLastStatusPoll = 0;
111 m_tLastConfigurationPoll = 0;
112 m_tLastRTUpdate = 0;
113 m_hPollerMutex = MutexCreate();
114 m_hAgentAccessMutex = MutexCreate();
115 m_mutexRTAccess = MutexCreate();
116 m_mutexTopoAccess = MutexCreate();
117 m_pAgentConnection = NULL;
118 m_szAgentVersion[0] = 0;
119 m_szPlatformName[0] = 0;
0ecc2200
VK
120 m_sysDescription = NULL;
121 m_sysName = NULL;
122 m_lldpNodeId = NULL;
5039dede
AK
123 m_dwNumParams = 0;
124 m_pParamList = NULL;
125 m_dwPollerNode = 0;
126 m_dwProxyNode = dwProxyNode;
127 m_dwSNMPProxy = dwSNMPProxy;
128 memset(m_qwLastEvents, 0, sizeof(QWORD) * MAX_LAST_EVENTS);
129 m_bIsHidden = TRUE;
130 m_pRoutingTable = NULL;
131 m_tFailTimeSNMP = 0;
132 m_tFailTimeAgent = 0;
040c45fa 133 m_linkLayerNeighbors = NULL;
0d75ea88 134 m_vrrpInfo = NULL;
5039dede 135 m_tLastTopologyPoll = 0;
040c45fa
VK
136 m_pTopology = NULL;
137 m_topologyRebuildTimestamp = 0;
5039dede
AK
138 m_iPendingStatus = -1;
139 m_iPollCount = 0;
140 m_iRequiredPollCount = 0; // Use system default
141 m_nUseIfXTable = IFXTABLE_DEFAULT; // Use system default
ab621f39 142 m_jobQueue = new ServerJobQueue();
f42b8099 143 m_fdb = NULL;
5039dede
AK
144}
145
146
147//
148// Node destructor
149//
150
151Node::~Node()
152{
153 MutexDestroy(m_hPollerMutex);
154 MutexDestroy(m_hAgentAccessMutex);
155 MutexDestroy(m_mutexRTAccess);
156 MutexDestroy(m_mutexTopoAccess);
157 delete m_pAgentConnection;
158 safe_free(m_pParamList);
159 DestroyRoutingTable(m_pRoutingTable);
a9ad9f61
VK
160 if (m_linkLayerNeighbors != NULL)
161 m_linkLayerNeighbors->decRefCount();
0d75ea88 162 delete m_vrrpInfo;
5039dede 163 delete m_pTopology;
ab621f39 164 delete m_jobQueue;
5d2c5741 165 delete m_snmpSecurity;
f42b8099
VK
166 if (m_fdb != NULL)
167 m_fdb->decRefCount();
5039dede
AK
168}
169
170
171//
172// Create object from database data
173//
174
175BOOL Node::CreateFromDB(DWORD dwId)
176{
5d2c5741 177 TCHAR query[1024];
5039dede
AK
178 DB_RESULT hResult;
179 int i, iNumRows;
180 DWORD dwSubnetId;
181 NetObj *pObject;
182 BOOL bResult = FALSE;
183
184 m_dwId = dwId;
185
186 if (!LoadCommonProperties())
187 {
35f836fe 188 DbgPrintf(2, _T("Cannot load common properties for node object %d"), dwId);
5039dede
AK
189 return FALSE;
190 }
191
5d2c5741
VK
192 _sntprintf(query, 1024, _T("SELECT primary_ip,node_flags,")
193 _T("snmp_version,auth_method,secret,")
194 _T("agent_port,status_poll_type,snmp_oid,")
195 _T("node_type,agent_version,")
196 _T("platform_name,poller_node_id,zone_guid,")
197 _T("proxy_node,snmp_proxy,required_polls,uname,")
65e2005b 198 _T("use_ifxtable,snmp_port,community,usm_auth_password,")
d74c80ea 199 _T("usm_priv_password,usm_methods,snmp_sys_name")
5d2c5741
VK
200 _T(" FROM nodes WHERE id=%d"), dwId);
201 hResult = DBSelect(g_hCoreDB, query);
e2babedf 202 if (hResult == NULL)
5039dede
AK
203 return FALSE; // Query failed
204
205 if (DBGetNumRows(hResult) == 0)
206 {
207 DBFreeResult(hResult);
5d2c5741 208 DbgPrintf(2, _T("Missing record in \"nodes\" table for node object %d"), dwId);
5039dede
AK
209 return FALSE;
210 }
211
212 m_dwIpAddr = DBGetFieldIPAddr(hResult, 0, 0);
213 m_dwFlags = DBGetFieldULong(hResult, 0, 1);
5d2c5741 214 m_snmpVersion = DBGetFieldLong(hResult, 0, 2);
5039dede
AK
215 m_wAuthMethod = (WORD)DBGetFieldLong(hResult, 0, 3);
216 DBGetField(hResult, 0, 4, m_szSharedSecret, MAX_SECRET_LENGTH);
5039dede
AK
217 m_wAgentPort = (WORD)DBGetFieldLong(hResult, 0, 5);
218 m_iStatusPollType = DBGetFieldLong(hResult, 0, 6);
5d2c5741 219 DBGetField(hResult, 0, 7, m_szObjectId, MAX_OID_LEN * 4);
5d2c5741
VK
220 m_dwNodeType = DBGetFieldULong(hResult, 0, 8);
221 DBGetField(hResult, 0, 9, m_szAgentVersion, MAX_AGENT_VERSION_LEN);
5d2c5741 222 DBGetField(hResult, 0, 10, m_szPlatformName, MAX_PLATFORM_NAME_LEN);
5d2c5741
VK
223 m_dwPollerNode = DBGetFieldULong(hResult, 0, 11);
224 m_dwZoneGUID = DBGetFieldULong(hResult, 0, 12);
225 m_dwProxyNode = DBGetFieldULong(hResult, 0, 13);
226 m_dwSNMPProxy = DBGetFieldULong(hResult, 0, 14);
227 m_iRequiredPollCount = DBGetFieldLong(hResult, 0, 15);
0ecc2200 228 m_sysDescription = DBGetField(hResult, 0, 16, NULL, 0);
5d2c5741 229 m_nUseIfXTable = (BYTE)DBGetFieldLong(hResult, 0, 17);
65e2005b 230 m_wSNMPPort = (WORD)DBGetFieldLong(hResult, 0, 18);
5039dede 231
5d2c5741 232 // SNMP authentication parameters
35f836fe
VK
233 char snmpAuthObject[256], snmpAuthPassword[256], snmpPrivPassword[256];
234 DBGetFieldA(hResult, 0, 19, snmpAuthObject, 256);
235 DBGetFieldA(hResult, 0, 20, snmpAuthPassword, 256);
236 DBGetFieldA(hResult, 0, 21, snmpPrivPassword, 256);
5d2c5741
VK
237 int snmpMethods = DBGetFieldLong(hResult, 0, 21);
238 delete m_snmpSecurity;
239 m_snmpSecurity = new SNMP_SecurityContext(snmpAuthObject, snmpAuthPassword, snmpPrivPassword, snmpMethods & 0xFF, snmpMethods >> 8);
14b98b3a 240 m_snmpSecurity->setSecurityModel((m_snmpVersion == SNMP_VERSION_3) ? SNMP_SECURITY_MODEL_USM : SNMP_SECURITY_MODEL_V2C);
5d2c5741 241
d74c80ea
VK
242 m_sysName = DBGetField(hResult, 0, 22, NULL, 0);
243
5039dede
AK
244 DBFreeResult(hResult);
245
246 if (!m_bIsDeleted)
247 {
248 // Link node to subnets
5d2c5741
VK
249 _sntprintf(query, 1024, _T("SELECT subnet_id FROM nsmap WHERE node_id=%d"), dwId);
250 hResult = DBSelect(g_hCoreDB, query);
5039dede
AK
251 if (hResult == NULL)
252 return FALSE; // Query failed
253
254 iNumRows = DBGetNumRows(hResult);
5039dede
AK
255 for(i = 0; i < iNumRows; i++)
256 {
257 dwSubnetId = DBGetFieldULong(hResult, i, 0);
258 pObject = FindObjectById(dwSubnetId);
259 if (pObject == NULL)
260 {
261 nxlog_write(MSG_INVALID_SUBNET_ID, EVENTLOG_ERROR_TYPE, "dd", dwId, dwSubnetId);
262 break;
263 }
264 else if (pObject->Type() != OBJECT_SUBNET)
265 {
266 nxlog_write(MSG_SUBNET_NOT_SUBNET, EVENTLOG_ERROR_TYPE, "dd", dwId, dwSubnetId);
267 break;
268 }
269 else
270 {
271 pObject->AddChild(this);
272 AddParent(pObject);
5039dede
AK
273 }
274 }
275
276 DBFreeResult(hResult);
7c521895 277 loadItemsFromDB();
5039dede
AK
278 LoadACLFromDB();
279
280 // Walk through all items in the node and load appropriate thresholds
a6c1f07f 281 bResult = TRUE;
5039dede 282 for(i = 0; i < (int)m_dwNumItems; i++)
fb05c05b 283 if (!m_ppItems[i]->loadThresholdsFromDB())
5039dede 284 {
5d2c5741 285 DbgPrintf(3, _T("Cannot load thresholds for DCI %d of node %d (%s)"),
fb05c05b 286 m_ppItems[i]->getId(), dwId, m_szName);
5039dede
AK
287 bResult = FALSE;
288 }
289 }
290 else
291 {
292 bResult = TRUE;
293 }
294
295 return bResult;
296}
297
298
299//
300// Save object to database
301//
302
303BOOL Node::SaveToDB(DB_HANDLE hdb)
304{
65e2005b 305 TCHAR szQuery[4096], szIpAddr[16];
5039dede
AK
306 DB_RESULT hResult;
307 BOOL bNewObject = TRUE;
308 BOOL bResult;
309
310 // Lock object's access
311 LockData();
312
313 SaveCommonProperties(hdb);
314
315 // Check for object's existence in database
65e2005b 316 _sntprintf(szQuery, 4096, _T("SELECT id FROM nodes WHERE id=%d"), m_dwId);
5039dede
AK
317 hResult = DBSelect(hdb, szQuery);
318 if (hResult != 0)
319 {
320 if (DBGetNumRows(hResult) > 0)
321 bNewObject = FALSE;
322 DBFreeResult(hResult);
323 }
324
325 // Form and execute INSERT or UPDATE query
4b7d8903 326 int snmpMethods = m_snmpSecurity->getAuthMethod() | (m_snmpSecurity->getPrivMethod() << 8);
5039dede 327 if (bNewObject)
65e2005b
VK
328 {
329 _sntprintf(szQuery, 4096,
330 _T("INSERT INTO nodes (id,primary_ip,snmp_port,")
331 _T("node_flags,snmp_version,community,status_poll_type,")
332 _T("agent_port,auth_method,secret,snmp_oid,proxy_node,")
333 _T("node_type,agent_version,platform_name,uname,")
334 _T("poller_node_id,zone_guid,snmp_proxy,required_polls,")
d74c80ea
VK
335 _T("use_ifxtable,usm_auth_password,usm_priv_password,usm_methods,snmp_sys_name) VALUES ")
336 _T("(%d,'%s',%d,%d,%d,%s,%d,%d,%d,%s,%s,%d,%d,%s,%s,%s,%d,%d,%d,%d,%d,%s,%s,%d,%s)"),
65e2005b 337 m_dwId, IpToStr(m_dwIpAddr, szIpAddr), (int)m_wSNMPPort, m_dwFlags,
35f836fe 338 m_snmpVersion, (const TCHAR *)DBPrepareStringA(hdb, m_snmpSecurity->getCommunity()),
65e2005b
VK
339 m_iStatusPollType, (int)m_wAgentPort, m_wAuthMethod,
340 (const TCHAR *)DBPrepareString(hdb, m_szSharedSecret),
341 (const TCHAR *)DBPrepareString(hdb, m_szObjectId),
342 m_dwProxyNode, m_dwNodeType, (const TCHAR *)DBPrepareString(hdb, m_szAgentVersion),
343 (const TCHAR *)DBPrepareString(hdb, m_szPlatformName),
0ecc2200 344 (const TCHAR *)DBPrepareString(hdb, m_sysDescription),
65e2005b 345 m_dwPollerNode, m_dwZoneGUID, m_dwSNMPProxy, m_iRequiredPollCount, m_nUseIfXTable,
35f836fe 346 (const TCHAR *)DBPrepareStringA(hdb, m_snmpSecurity->getAuthPassword()),
d74c80ea
VK
347 (const TCHAR *)DBPrepareStringA(hdb, m_snmpSecurity->getPrivPassword()), snmpMethods,
348 (const TCHAR *)DBPrepareString(hdb, m_sysName));
65e2005b 349 }
5039dede 350 else
65e2005b
VK
351 {
352 _sntprintf(szQuery, 4096,
353 _T("UPDATE nodes SET primary_ip='%s',snmp_port=%d,")
354 _T("node_flags=%d,snmp_version=%d,community=%s,")
355 _T("status_poll_type=%d,agent_port=%d,auth_method=%d,secret=%s,")
356 _T("snmp_oid=%s,node_type=%d,uname=%s,")
357 _T("agent_version=%s,platform_name=%s,poller_node_id=%d,")
358 _T("zone_guid=%d,proxy_node=%d,snmp_proxy=%d,")
359 _T("required_polls=%d,use_ifxtable=%d,usm_auth_password=%s,")
d74c80ea 360 _T("usm_priv_password=%s,usm_methods=%d,snmp_sys_name=%s WHERE id=%d"),
65e2005b 361 IpToStr(m_dwIpAddr, szIpAddr), m_wSNMPPort,
35f836fe 362 m_dwFlags, m_snmpVersion, (const TCHAR *)DBPrepareStringA(hdb, m_snmpSecurity->getCommunity()),
65e2005b
VK
363 m_iStatusPollType, m_wAgentPort, m_wAuthMethod,
364 (const TCHAR *)DBPrepareString(hdb, m_szSharedSecret),
365 (const TCHAR *)DBPrepareString(hdb, m_szObjectId), m_dwNodeType,
0ecc2200 366 (const TCHAR *)DBPrepareString(hdb, m_sysDescription),
65e2005b
VK
367 (const TCHAR *)DBPrepareString(hdb, m_szAgentVersion),
368 (const TCHAR *)DBPrepareString(hdb, m_szPlatformName), m_dwPollerNode, m_dwZoneGUID,
369 m_dwProxyNode, m_dwSNMPProxy, m_iRequiredPollCount,
35f836fe 370 m_nUseIfXTable, (const TCHAR *)DBPrepareStringA(hdb, m_snmpSecurity->getAuthPassword()),
d74c80ea
VK
371 (const TCHAR *)DBPrepareStringA(hdb, m_snmpSecurity->getPrivPassword()), snmpMethods,
372 (const TCHAR *)DBPrepareString(hdb, m_sysName), m_dwId);
65e2005b 373 }
5039dede 374 bResult = DBQuery(hdb, szQuery);
5039dede 375
7c521895
VK
376 // Save access list
377 SaveACLToDB(hdb);
378
379 UnlockData();
380
5039dede
AK
381 // Save data collection items
382 if (bResult)
383 {
384 DWORD i;
385
7c521895 386 lockDciAccess();
5039dede 387 for(i = 0; i < m_dwNumItems; i++)
fb05c05b 388 m_ppItems[i]->saveToDB(hdb);
7c521895 389 unlockDciAccess();
5039dede
AK
390 }
391
7c521895
VK
392 // Clear modifications flag
393 LockData();
5039dede 394 m_bIsModified = FALSE;
7c521895 395 UnlockData();
5039dede 396
7c521895 397 return bResult;
5039dede
AK
398}
399
400
401//
402// Delete object from database
403//
404
65e2005b 405BOOL Node::DeleteFromDB()
5039dede 406{
35f836fe 407 TCHAR szQuery[256];
5039dede
AK
408 BOOL bSuccess;
409
410 bSuccess = Template::DeleteFromDB();
411 if (bSuccess)
412 {
65e2005b 413 _sntprintf(szQuery, 256, _T("DELETE FROM nodes WHERE id=%d"), m_dwId);
5039dede 414 QueueSQLRequest(szQuery);
65e2005b 415 _sntprintf(szQuery, 256, _T("DELETE FROM nsmap WHERE node_id=%d"), m_dwId);
5039dede 416 QueueSQLRequest(szQuery);
65e2005b 417 _sntprintf(szQuery, 256, _T("DROP TABLE idata_%d"), m_dwId);
5039dede
AK
418 QueueSQLRequest(szQuery);
419 }
420 return bSuccess;
421}
422
423
424//
425// Get ARP cache from node
426//
427
7c521895 428ARP_CACHE *Node::getArpCache()
5039dede
AK
429{
430 ARP_CACHE *pArpCache = NULL;
431
432 if (m_dwFlags & NF_IS_LOCAL_MGMT)
433 {
434 pArpCache = GetLocalArpCache();
435 }
436 else if (m_dwFlags & NF_IS_NATIVE_AGENT)
437 {
7c521895
VK
438 agentLock();
439 if (connectToAgent())
4687826e 440 pArpCache = m_pAgentConnection->getArpCache();
7c521895 441 agentUnlock();
5039dede
AK
442 }
443 else if (m_dwFlags & NF_IS_SNMP)
444 {
445 SNMP_Transport *pTransport;
446
cd9f247e 447 pTransport = createSnmpTransport();
803d47be
VK
448 if (pTransport != NULL)
449 {
450 pArpCache = SnmpGetArpCache(m_snmpVersion, pTransport);
451 delete pTransport;
452 }
5039dede
AK
453 }
454
455 return pArpCache;
456}
457
458
459//
460// Get list of interfaces from node
461//
462
98762401 463InterfaceList *Node::getInterfaceList()
5039dede 464{
98762401 465 InterfaceList *pIfList = NULL;
5039dede 466
b741f151 467 if ((m_dwFlags & NF_IS_NATIVE_AGENT) && (!(m_dwFlags & NF_DISABLE_NXCP)))
5039dede 468 {
7c521895
VK
469 agentLock();
470 if (connectToAgent())
5039dede 471 {
4687826e 472 pIfList = m_pAgentConnection->getInterfaceList();
98762401 473 pIfList->removeLoopbacks();
5039dede 474 }
7c521895 475 agentUnlock();
5039dede 476 }
b741f151
VK
477 if ((pIfList == NULL) && (m_dwFlags & NF_IS_LOCAL_MGMT))
478 {
479 pIfList = GetLocalInterfaceList();
480 }
5039dede
AK
481 if ((pIfList == NULL) && (m_dwFlags & NF_IS_SNMP) &&
482 (!(m_dwFlags & NF_DISABLE_SNMP)))
483 {
484 SNMP_Transport *pTransport;
485 BOOL useIfXTable;
486
cd9f247e 487 pTransport = createSnmpTransport();
803d47be 488 if (pTransport != NULL)
5039dede 489 {
803d47be
VK
490 if (m_nUseIfXTable == IFXTABLE_DEFAULT)
491 {
492 useIfXTable = (ConfigReadInt(_T("UseIfXTable"), 1) != 0);
493 }
494 else
495 {
496 useIfXTable = (m_nUseIfXTable == IFXTABLE_ENABLED);
497 }
ae2ef6f9 498 pIfList = SnmpGetInterfaceList(m_snmpVersion, pTransport, this, useIfXTable);
eec253a8 499
8792fe27 500 if ((pIfList != NULL) && (m_dwFlags & NF_IS_BRIDGE))
eec253a8
VK
501 {
502 BridgeMapPorts(m_snmpVersion, pTransport, pIfList);
503 }
803d47be 504 delete pTransport;
5039dede 505 }
5039dede
AK
506 }
507
508 if (pIfList != NULL)
0d75ea88 509 {
5039dede 510 CheckInterfaceNames(pIfList);
0d75ea88
VK
511 addVrrpInterfaces(pIfList);
512 }
5039dede
AK
513
514 return pIfList;
515}
516
517
518//
0d75ea88
VK
519// Add VRRP interfaces to interface list
520//
521
98762401 522void Node::addVrrpInterfaces(InterfaceList *ifList)
0d75ea88
VK
523{
524 int i, j, k;
525 TCHAR buffer[32];
526
527 LockData();
528 if (m_vrrpInfo != NULL)
529 {
530 DbgPrintf(6, _T("Node::addVrrpInterfaces(node=%s [%d]): m_vrrpInfo->getSize()=%d"), m_szName, (int)m_dwId, m_vrrpInfo->getSize());
531
532 for(i = 0; i < m_vrrpInfo->getSize(); i++)
533 {
534 VrrpRouter *router = m_vrrpInfo->getRouter(i);
535 DbgPrintf(6, _T("Node::addVrrpInterfaces(node=%s [%d]): vrouter %d state=%d"), m_szName, (int)m_dwId, i, router->getState());
536 if (router->getState() != VRRP_STATE_MASTER)
537 continue; // Do not add interfaces if router is not in master state
538
539 // Get netmask for this VR
540 DWORD netmask = 0;
98762401
VK
541 for(j = 0; j < ifList->getSize(); j++)
542 if (ifList->get(j)->dwIndex == router->getIfIndex())
0d75ea88 543 {
98762401 544 netmask = ifList->get(j)->dwIpNetMask;
0d75ea88
VK
545 break;
546 }
547
548 // Walk through all VR virtual IPs
549 for(j = 0; j < router->getVipCount(); j++)
550 {
551 DWORD vip = router->getVip(j);
552 DbgPrintf(6, _T("Node::addVrrpInterfaces(node=%s [%d]): checking VIP %s@%d"), m_szName, (int)m_dwId, IpToStr(vip, buffer), i);
553 if (vip != 0)
554 {
98762401
VK
555 for(k = 0; k < ifList->getSize(); k++)
556 if (ifList->get(k)->dwIpAddr == vip)
0d75ea88 557 break;
98762401 558 if (k == ifList->getSize())
0d75ea88 559 {
98762401
VK
560 INTERFACE_INFO iface;
561 memset(&iface, 0, sizeof(INTERFACE_INFO));
562 _sntprintf(iface.szName, MAX_DB_STRING, _T("vrrp.%u.%u.%d"), router->getId(), router->getIfIndex(), j);
563 memcpy(iface.bMacAddr, router->getVirtualMacAddr(), MAC_ADDR_LENGTH);
564 iface.dwIpAddr = vip;
565 iface.dwIpNetMask = netmask;
566 ifList->add(&iface);
567 DbgPrintf(6, _T("Node::addVrrpInterfaces(node=%s [%d]): added interface %s"), m_szName, (int)m_dwId, iface.szName);
0d75ea88
VK
568 }
569 }
570 }
571 }
572 }
573 UnlockData();
574}
575
576
577//
5039dede
AK
578// Find interface by index and node IP
579// Returns pointer to interface object or NULL if appropriate interface couldn't be found
580//
581
58b3e451 582Interface *Node::findInterface(DWORD dwIndex, DWORD dwHostAddr)
5039dede
AK
583{
584 DWORD i;
585 Interface *pInterface;
586
587 LockChildList(FALSE);
588 for(i = 0; i < m_dwChildCount; i++)
589 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
590 {
591 pInterface = (Interface *)m_pChildList[i];
eec253a8 592 if (pInterface->getIfIndex() == dwIndex)
5039dede 593 {
eec253a8
VK
594 if (((pInterface->IpAddr() & pInterface->getIpNetMask()) ==
595 (dwHostAddr & pInterface->getIpNetMask())) ||
5039dede
AK
596 (dwHostAddr == INADDR_ANY))
597 {
598 UnlockChildList();
599 return pInterface;
600 }
601 }
602 }
603 UnlockChildList();
604 return NULL;
605}
606
607
608//
630e15d6
VK
609// Find interface by name
610// Returns pointer to interface object or NULL if appropriate interface couldn't be found
611//
612
613Interface *Node::findInterface(const TCHAR *name)
614{
615 DWORD i;
616 Interface *pInterface;
617
618 LockChildList(FALSE);
619 for(i = 0; i < m_dwChildCount; i++)
620 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
621 {
622 pInterface = (Interface *)m_pChildList[i];
623 if (!_tcsicmp(pInterface->Name(), name))
624 {
625 UnlockChildList();
626 return pInterface;
627 }
628 }
629 UnlockChildList();
630 return NULL;
631}
632
633
634//
3f8c54e7
VK
635// Find interface by slot/port pair
636// Returns pointer to interface object or NULL if appropriate interface couldn't be found
637//
638
639Interface *Node::findInterfaceBySlotAndPort(DWORD slot, DWORD port)
640{
641 DWORD i;
642 Interface *pInterface;
643
644 if ((slot == 0) || (port == 0))
645 return NULL;
646
647 LockChildList(FALSE);
648 for(i = 0; i < m_dwChildCount; i++)
649 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
650 {
651 pInterface = (Interface *)m_pChildList[i];
652 if ((pInterface->getSlotNumber() == slot) && (pInterface->getPortNumber() == port))
653 {
654 UnlockChildList();
655 return pInterface;
656 }
657 }
658 UnlockChildList();
659 return NULL;
660}
661
662
663//
eec253a8
VK
664// Find interface by MAC address
665// Returns pointer to interface object or NULL if appropriate interface couldn't be found
666//
667
668Interface *Node::findInterfaceByMAC(const BYTE *macAddr)
669{
670 DWORD i;
671 Interface *pInterface;
672
673 LockChildList(FALSE);
674 for(i = 0; i < m_dwChildCount; i++)
675 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
676 {
677 pInterface = (Interface *)m_pChildList[i];
678 if (!memcmp(pInterface->getMacAddr(), macAddr, MAC_ADDR_LENGTH))
679 {
680 UnlockChildList();
681 return pInterface;
682 }
683 }
684 UnlockChildList();
685 return NULL;
686}
687
688
689//
690// Find interface by IP address
691// Returns pointer to interface object or NULL if appropriate interface couldn't be found
692//
693
694Interface *Node::findInterfaceByIP(DWORD ipAddr)
695{
696 DWORD i;
697 Interface *pInterface;
698
699 if (ipAddr == 0)
700 return NULL;
701
702 LockChildList(FALSE);
703 for(i = 0; i < m_dwChildCount; i++)
704 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
705 {
706 pInterface = (Interface *)m_pChildList[i];
707 if (pInterface->IpAddr() == ipAddr)
708 {
709 UnlockChildList();
710 return pInterface;
711 }
712 }
713 UnlockChildList();
714 return NULL;
715}
716
717
718//
719// Find interface by bridge port number
720//
721
722Interface *Node::findBridgePort(DWORD bridgePortNumber)
723{
724 DWORD i;
725 Interface *pInterface;
726
727 LockChildList(FALSE);
728 for(i = 0; i < m_dwChildCount; i++)
729 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
730 {
731 pInterface = (Interface *)m_pChildList[i];
732 if (pInterface->getBridgePortNumber() == bridgePortNumber)
733 {
734 UnlockChildList();
735 return pInterface;
736 }
737 }
738 UnlockChildList();
739 return NULL;
740}
741
742
743//
f42b8099
VK
744// Find connection point for node
745//
746
29272e65 747Interface *Node::findConnectionPoint(DWORD *localIfId, BYTE *localMacAddr)
f42b8099
VK
748{
749 Interface *cp = NULL;
750 LockChildList(FALSE);
751 for(DWORD i = 0; i < m_dwChildCount; i++)
752 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
753 {
754 Interface *iface = (Interface *)m_pChildList[i];
eec253a8 755 cp = FindInterfaceConnectionPoint(iface->getMacAddr());
f42b8099 756 if (cp != NULL)
06a93345
VK
757 {
758 *localIfId = iface->Id();
eec253a8 759 memcpy(localMacAddr, iface->getMacAddr(), MAC_ADDR_LENGTH);
f42b8099 760 break;
06a93345 761 }
f42b8099
VK
762 }
763 UnlockChildList();
764 return cp;
765}
766
767
768//
5039dede
AK
769// Check if given IP address is one of node's interfaces
770//
771
58b3e451 772BOOL Node::isMyIP(DWORD dwIpAddr)
5039dede
AK
773{
774 DWORD i;
775
776 LockChildList(FALSE);
777 for(i = 0; i < m_dwChildCount; i++)
778 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
779 {
780 if (((Interface *)m_pChildList[i])->IpAddr() == dwIpAddr)
781 {
782 UnlockChildList();
783 return TRUE;
784 }
785 }
786 UnlockChildList();
787 return FALSE;
788}
789
790
791//
792// Create new interface
793//
794
eec253a8 795void Node::createNewInterface(DWORD dwIpAddr, DWORD dwNetMask, const TCHAR *name,
76f9abfd
VK
796 DWORD dwIndex, DWORD dwType, BYTE *pbMacAddr, DWORD bridgePort,
797 DWORD slot, DWORD port)
5039dede
AK
798{
799 Interface *pInterface;
800 Subnet *pSubnet = NULL;
801 Cluster *pCluster;
eec253a8 802 bool bAddToSubnet, bSyntheticMask = false;
5039dede 803
f7694811
VK
804 DbgPrintf(5, _T("Node::createNewInterface(%08X, %08X, %s, %d, %d, bp=%d, slot=%d, port=%d) called for node %s [%d]"),
805 dwIpAddr, dwNetMask, CHECK_NULL(name), dwIndex, dwType, bridgePort, slot, port, m_szName, m_dwId);
5039dede
AK
806
807 // Find subnet to place interface object to
808 if (dwIpAddr != 0)
809 {
7c521895 810 pCluster = getMyCluster();
58b3e451 811 bAddToSubnet = (pCluster != NULL) ? !pCluster->isSyncAddr(dwIpAddr) : TRUE;
f7694811 812 DbgPrintf(5, _T("Node::createNewInterface: node=%s [%d] cluster=%s [%d] add=%d"),
5039dede
AK
813 m_szName, m_dwId, (pCluster != NULL) ? pCluster->Name() : _T("(null)"),
814 (pCluster != NULL) ? pCluster->Id() : 0, bAddToSubnet);
815 if (bAddToSubnet)
816 {
817 pSubnet = FindSubnetForNode(dwIpAddr);
818 if (pSubnet == NULL)
819 {
820 // Check if netmask is 0 (detect), and if yes, create
821 // new subnet with class mask
822 if (dwNetMask == 0)
823 {
824 bSyntheticMask = TRUE;
825 if (dwIpAddr < 0xE0000000)
826 {
827 dwNetMask = 0xFFFFFF00; // Class A, B or C
828 }
829 else
830 {
831 TCHAR szBuffer[16];
832
833 // Multicast address??
834 DbgPrintf(2, _T("Attempt to create interface object with multicast address %s"),
835 IpToStr(dwIpAddr, szBuffer));
836 }
837 }
838
839 // Create new subnet object
840 if (dwIpAddr < 0xE0000000)
841 {
842 pSubnet = new Subnet(dwIpAddr & dwNetMask, dwNetMask, m_dwZoneGUID, bSyntheticMask);
843 NetObjInsert(pSubnet, TRUE);
844 g_pEntireNet->AddSubnet(pSubnet);
845 }
846 }
847 else
848 {
849 // Set correct netmask if we was asked for it
850 if (dwNetMask == 0)
851 {
eec253a8
VK
852 dwNetMask = pSubnet->getIpNetMask();
853 bSyntheticMask = pSubnet->isSyntheticMask();
5039dede
AK
854 }
855 }
856 }
857 }
858
859 // Create interface object
35f836fe
VK
860 if (name != NULL)
861 pInterface = new Interface(name, dwIndex, dwIpAddr, dwNetMask, dwType);
5039dede
AK
862 else
863 pInterface = new Interface(dwIpAddr, dwNetMask, bSyntheticMask);
864 if (pbMacAddr != NULL)
eec253a8
VK
865 pInterface->setMacAddr(pbMacAddr);
866 pInterface->setBridgePortNumber(bridgePort);
76f9abfd
VK
867 pInterface->setSlotNumber(slot);
868 pInterface->setPortNumber(port);
5039dede
AK
869
870 // Insert to objects' list and generate event
871 NetObjInsert(pInterface, TRUE);
eec253a8 872 addInterface(pInterface);
5039dede
AK
873 if (!m_bIsHidden)
874 pInterface->Unhide();
875 PostEvent(EVENT_INTERFACE_ADDED, m_dwId, "dsaad", pInterface->Id(),
876 pInterface->Name(), pInterface->IpAddr(),
eec253a8 877 pInterface->getIpNetMask(), pInterface->getIfIndex());
5039dede
AK
878
879 // Bind node to appropriate subnet
880 if (pSubnet != NULL)
881 {
882 pSubnet->AddNode(this);
883
884 // Check if subnet mask is correct on interface
eec253a8 885 if ((pSubnet->getIpNetMask() != pInterface->getIpNetMask()) && !pSubnet->isSyntheticMask())
5039dede
AK
886 {
887 PostEvent(EVENT_INCORRECT_NETMASK, m_dwId, "idsaa", pInterface->Id(),
eec253a8
VK
888 pInterface->getIfIndex(), pInterface->Name(),
889 pInterface->getIpNetMask(), pSubnet->getIpNetMask());
5039dede
AK
890 }
891 }
892}
893
894
895//
896// Delete interface from node
897//
898
eec253a8 899void Node::deleteInterface(Interface *pInterface)
5039dede
AK
900{
901 DWORD i;
902
dfb38baf
VK
903 DbgPrintf(5, _T("Node::deleteInterface(node=%s [%d], interface=%s [%d])"), m_szName, m_dwId, pInterface->Name(), pInterface->Id());
904
5039dede
AK
905 // Check if we should unlink node from interface's subnet
906 if (pInterface->IpAddr() != 0)
907 {
908 BOOL bUnlink = TRUE;
909
910 LockChildList(FALSE);
911 for(i = 0; i < m_dwChildCount; i++)
912 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
913 if (m_pChildList[i] != pInterface)
eec253a8
VK
914 if ((((Interface *)m_pChildList[i])->IpAddr() & ((Interface *)m_pChildList[i])->getIpNetMask()) ==
915 (pInterface->IpAddr() & pInterface->getIpNetMask()))
5039dede
AK
916 {
917 bUnlink = FALSE;
918 break;
919 }
920 UnlockChildList();
921
922 if (bUnlink)
923 {
924 // Last interface in subnet, should unlink node
eec253a8 925 Subnet *pSubnet = FindSubnetByIP(pInterface->IpAddr() & pInterface->getIpNetMask());
5039dede
AK
926 if (pSubnet != NULL)
927 {
928 DeleteParent(pSubnet);
929 pSubnet->DeleteChild(this);
930 }
dfb38baf
VK
931 DbgPrintf(5, _T("Node::deleteInterface(node=%s [%d], interface=%s [%d]): unlinked from subnet %s [%d]"),
932 m_szName, m_dwId, pInterface->Name(), pInterface->Id(), pSubnet->Name(), pSubnet->Id());
5039dede
AK
933 }
934 }
935 pInterface->Delete(FALSE);
936}
937
938
939//
940// Calculate node status based on child objects status
941//
942
943void Node::CalculateCompoundStatus(BOOL bForcedRecalc)
944{
945 int iOldStatus = m_iStatus;
946 static DWORD dwEventCodes[] = { EVENT_NODE_NORMAL, EVENT_NODE_MINOR,
947 EVENT_NODE_WARNING, EVENT_NODE_MAJOR, EVENT_NODE_CRITICAL,
948 EVENT_NODE_UNKNOWN, EVENT_NODE_UNMANAGED };
949
950 NetObj::CalculateCompoundStatus(bForcedRecalc);
951 if (m_iStatus != iOldStatus)
952 PostEvent(dwEventCodes[m_iStatus], m_dwId, "d", iOldStatus);
953}
954
955
956//
957// Perform status poll on node
958//
959
58b3e451 960void Node::statusPoll(ClientSession *pSession, DWORD dwRqId, int nPoller)
5039dede
AK
961{
962 DWORD i, dwPollListSize, dwOldFlags = m_dwFlags;
963 NetObj *pPollerNode = NULL, **ppPollList;
964 BOOL bAllDown;
965 Queue *pQueue; // Delayed event queue
966 SNMP_Transport *pTransport;
967 Cluster *pCluster;
968 time_t tNow, tExpire;
969
970 pQueue = new Queue;
35f836fe 971 SetPollerInfo(nPoller, _T("wait for lock"));
7c521895 972 pollerLock();
5039dede 973 m_pPollRequestor = pSession;
35f836fe
VK
974 SendPollerMsg(dwRqId, _T("Starting status poll for node %s\r\n"), m_szName);
975 DbgPrintf(5, _T("Starting status poll for node %s (ID: %d)"), m_szName, m_dwId);
5039dede
AK
976
977 // Read capability expiration time and current time
978 tExpire = (time_t)ConfigReadULong(_T("CapabilityExpirationTime"), 604800);
979 tNow = time(NULL);
980
981 // Check SNMP agent connectivity
982restart_agent_check:
15d6f8c9 983 if ((m_dwFlags & NF_IS_SNMP) && (!(m_dwFlags & NF_DISABLE_SNMP)) && (m_dwIpAddr != 0))
5039dede
AK
984 {
985 TCHAR szBuffer[256];
986 DWORD dwResult;
987
35f836fe 988 DbgPrintf(6, _T("StatusPoll(%s): check SNMP"), m_szName);
cd9f247e 989 pTransport = createSnmpTransport();
803d47be
VK
990 if (pTransport == NULL)
991 {
35f836fe 992 DbgPrintf(6, _T("StatusPoll(%s): cannot create SNMP transport"), m_szName);
803d47be
VK
993 goto skip_snmp_check;
994 }
995
35f836fe
VK
996 SetPollerInfo(nPoller, _T("check SNMP"));
997 SendPollerMsg(dwRqId, _T("Checking SNMP agent connectivity\r\n"));
f42b8099 998 dwResult = SnmpGet(m_snmpVersion, pTransport, _T(".1.3.6.1.2.1.1.2.0"), NULL, 0, szBuffer, 256, 0);
5039dede
AK
999 if ((dwResult == SNMP_ERR_SUCCESS) || (dwResult == SNMP_ERR_NO_OBJECT))
1000 {
1001 if (m_dwDynamicFlags & NDF_SNMP_UNREACHABLE)
1002 {
1003 m_dwDynamicFlags &= ~NDF_SNMP_UNREACHABLE;
1004 PostEventEx(pQueue, EVENT_SNMP_OK, m_dwId, NULL);
35f836fe 1005 SendPollerMsg(dwRqId, POLLER_INFO _T("Connectivity with SNMP agent restored\r\n"));
5039dede
AK
1006 }
1007 }
1008 else
1009 {
35f836fe 1010 SendPollerMsg(dwRqId, POLLER_ERROR _T("SNMP agent unreachable\r\n"));
5039dede
AK
1011 if (m_dwDynamicFlags & NDF_SNMP_UNREACHABLE)
1012 {
1013 if ((tNow > m_tFailTimeSNMP + tExpire) &&
1014 (!(m_dwDynamicFlags & NDF_UNREACHABLE)))
1015 {
1016 m_dwFlags &= ~NF_IS_SNMP;
1017 m_dwDynamicFlags &= ~NDF_SNMP_UNREACHABLE;
1018 m_szObjectId[0] = 0;
35f836fe 1019 SendPollerMsg(dwRqId, POLLER_WARNING _T("Attribute isSNMP set to FALSE\r\n"));
5039dede
AK
1020 }
1021 }
1022 else
1023 {
1024 m_dwDynamicFlags |= NDF_SNMP_UNREACHABLE;
1025 PostEventEx(pQueue, EVENT_SNMP_FAIL, m_dwId, NULL);
1026 m_tFailTimeSNMP = tNow;
1027 }
1028 }
1029 delete pTransport;
35f836fe 1030 DbgPrintf(6, _T("StatusPoll(%s): SNMP check finished"), m_szName);
5039dede
AK
1031 }
1032
803d47be 1033skip_snmp_check:
5039dede 1034 // Check native agent connectivity
15d6f8c9 1035 if ((m_dwFlags & NF_IS_NATIVE_AGENT) && (!(m_dwFlags & NF_DISABLE_NXCP)) && (m_dwIpAddr != 0))
5039dede 1036 {
35f836fe
VK
1037 DbgPrintf(6, _T("StatusPoll(%s): checking agent"), m_szName);
1038 SetPollerInfo(nPoller, _T("check agent"));
1039 SendPollerMsg(dwRqId, _T("Checking NetXMS agent connectivity\r\n"));
c3acd0f6
VK
1040
1041 DWORD error, socketError;
a4569c4d
VK
1042 agentLock();
1043 if (connectToAgent(&error, &socketError))
5039dede 1044 {
35f836fe 1045 DbgPrintf(7, _T("StatusPoll(%s): connected to agent"), m_szName);
5039dede
AK
1046 if (m_dwDynamicFlags & NDF_AGENT_UNREACHABLE)
1047 {
1048 m_dwDynamicFlags &= ~NDF_AGENT_UNREACHABLE;
1049 PostEventEx(pQueue, EVENT_AGENT_OK, m_dwId, NULL);
35f836fe 1050 SendPollerMsg(dwRqId, POLLER_INFO _T("Connectivity with NetXMS agent restored\r\n"));
5039dede 1051 }
5039dede
AK
1052 }
1053 else
1054 {
35f836fe
VK
1055 DbgPrintf(6, _T("StatusPoll(%s): agent unreachable, error=%d, socketError=%d"), m_szName, (int)error, (int)socketError);
1056 SendPollerMsg(dwRqId, POLLER_ERROR _T("NetXMS agent unreachable\r\n"));
5039dede
AK
1057 if (m_dwDynamicFlags & NDF_AGENT_UNREACHABLE)
1058 {
1059 if ((tNow > m_tFailTimeAgent + tExpire) &&
1060 (!(m_dwDynamicFlags & NDF_UNREACHABLE)))
1061 {
1062 m_dwFlags &= ~NF_IS_NATIVE_AGENT;
1063 m_dwDynamicFlags &= ~NDF_AGENT_UNREACHABLE;
1064 m_szPlatformName[0] = 0;
1065 m_szAgentVersion[0] = 0;
35f836fe 1066 SendPollerMsg(dwRqId, POLLER_WARNING _T("Attribute isNetXMSAgent set to FALSE\r\n"));
5039dede
AK
1067 }
1068 }
1069 else
1070 {
1071 m_dwDynamicFlags |= NDF_AGENT_UNREACHABLE;
1072 PostEventEx(pQueue, EVENT_AGENT_FAIL, m_dwId, NULL);
1073 m_tFailTimeAgent = tNow;
1074 }
1075 }
a4569c4d 1076 agentUnlock();
35f836fe 1077 DbgPrintf(7, _T("StatusPoll(%s): agent check finished"), m_szName);
5039dede
AK
1078 }
1079
35f836fe 1080 SetPollerInfo(nPoller, _T("prepare polling list"));
5039dede
AK
1081
1082 // Find service poller node object
1083 LockData();
1084 if (m_dwPollerNode != 0)
1085 {
1086 pPollerNode = FindObjectById(m_dwPollerNode);
1087 if (pPollerNode != NULL)
1088 {
1089 if (pPollerNode->Type() != OBJECT_NODE)
1090 pPollerNode = NULL;
1091 }
1092 }
1093 UnlockData();
1094
1095 // If nothing found, use management server
1096 if (pPollerNode == NULL)
1097 {
1098 pPollerNode = FindObjectById(g_dwMgmtNode);
1099 if (pPollerNode != NULL)
1100 pPollerNode->IncRefCount();
1101 }
1102 else
1103 {
1104 pPollerNode->IncRefCount();
1105 }
1106
1107 // Create polling list
1108 ppPollList = (NetObj **)malloc(sizeof(NetObj *) * m_dwChildCount);
1109 LockChildList(FALSE);
1110 for(i = 0, dwPollListSize = 0; i < m_dwChildCount; i++)
1111 if (m_pChildList[i]->Status() != STATUS_UNMANAGED)
1112 {
1113 m_pChildList[i]->IncRefCount();
1114 ppPollList[dwPollListSize++] = m_pChildList[i];
1115 }
1116 UnlockChildList();
1117
1118 // Poll interfaces and services
35f836fe
VK
1119 SetPollerInfo(nPoller, _T("child poll"));
1120 DbgPrintf(7, _T("StatusPoll(%s): starting child object poll"), m_szName);
7c521895 1121 pCluster = getMyCluster();
cd9f247e 1122 pTransport = createSnmpTransport();
5039dede
AK
1123 for(i = 0; i < dwPollListSize; i++)
1124 {
1125 switch(ppPollList[i]->Type())
1126 {
1127 case OBJECT_INTERFACE:
35f836fe 1128 DbgPrintf(7, _T("StatusPoll(%s): polling interface %d [%s]"), m_szName, ppPollList[i]->Id(), ppPollList[i]->Name());
5039dede 1129 ((Interface *)ppPollList[i])->StatusPoll(pSession, dwRqId, pQueue,
58b3e451 1130 (pCluster != NULL) ? pCluster->isSyncAddr(((Interface *)ppPollList[i])->IpAddr()) : FALSE,
5039dede
AK
1131 pTransport);
1132 break;
1133 case OBJECT_NETWORKSERVICE:
35f836fe 1134 DbgPrintf(7, _T("StatusPoll(%s): polling network service %d [%s]"), m_szName, ppPollList[i]->Id(), ppPollList[i]->Name());
5039dede
AK
1135 ((NetworkService *)ppPollList[i])->StatusPoll(pSession, dwRqId,
1136 (Node *)pPollerNode, pQueue);
1137 break;
1138 default:
1139 break;
1140 }
1141 ppPollList[i]->DecRefCount();
1142 }
1143 delete pTransport;
1144 safe_free(ppPollList);
35f836fe 1145 DbgPrintf(7, _T("StatusPoll(%s): finished child object poll"), m_szName);
5039dede
AK
1146
1147 // Check if entire node is down
15d6f8c9
VK
1148 // This check is disabled for nodes without IP address
1149 if (m_dwIpAddr != 0)
5039dede 1150 {
15d6f8c9
VK
1151 LockChildList(FALSE);
1152 if (m_dwChildCount > 0)
1153 {
1154 for(i = 0, bAllDown = TRUE; i < m_dwChildCount; i++)
1155 if ((m_pChildList[i]->Type() == OBJECT_INTERFACE) &&
1156 (m_pChildList[i]->Status() != STATUS_CRITICAL) &&
1157 (m_pChildList[i]->Status() != STATUS_UNKNOWN) &&
1158 (m_pChildList[i]->Status() != STATUS_UNMANAGED) &&
1159 (m_pChildList[i]->Status() != STATUS_DISABLED))
1160 {
1161 bAllDown = FALSE;
1162 break;
1163 }
1164 }
1165 else
1166 {
1167 bAllDown = FALSE;
1168 }
1169 UnlockChildList();
1170 if (bAllDown && (m_dwFlags & NF_IS_NATIVE_AGENT) &&
1171 (!(m_dwFlags & NF_DISABLE_NXCP)))
1172 if (!(m_dwDynamicFlags & NDF_AGENT_UNREACHABLE))
1173 bAllDown = FALSE;
1174 if (bAllDown && (m_dwFlags & NF_IS_SNMP) &&
1175 (!(m_dwFlags & NF_DISABLE_SNMP)))
1176 if (!(m_dwDynamicFlags & NDF_SNMP_UNREACHABLE))
1177 bAllDown = FALSE;
1178 if (bAllDown)
1179 {
1180 if (!(m_dwDynamicFlags & NDF_UNREACHABLE))
1181 {
1182 m_dwDynamicFlags |= NDF_UNREACHABLE;
1183 PostEvent(EVENT_NODE_DOWN, m_dwId, NULL);
35f836fe 1184 SendPollerMsg(dwRqId, POLLER_ERROR _T("Node is unreachable\r\n"));
15d6f8c9
VK
1185 }
1186 else
1187 {
35f836fe 1188 SendPollerMsg(dwRqId, POLLER_WARNING _T("Node is still unreachable\r\n"));
15d6f8c9
VK
1189 }
1190 }
1191 else
1192 {
1193 if (m_dwDynamicFlags & NDF_UNREACHABLE)
1194 {
1195 m_dwDynamicFlags &= ~(NDF_UNREACHABLE | NDF_SNMP_UNREACHABLE | NDF_AGENT_UNREACHABLE);
1196 PostEvent(EVENT_NODE_UP, m_dwId, NULL);
35f836fe 1197 SendPollerMsg(dwRqId, POLLER_INFO _T("Node recovered from unreachable state\r\n"));
15d6f8c9
VK
1198 goto restart_agent_check;
1199 }
1200 else
1201 {
35f836fe 1202 SendPollerMsg(dwRqId, POLLER_INFO _T("Node is connected\r\n"));
15d6f8c9
VK
1203 }
1204 }
5039dede 1205 }
5039dede
AK
1206
1207 // Send delayed events and destroy delayed event queue
1208 ResendEvents(pQueue);
1209 delete pQueue;
1210
35f836fe 1211 SetPollerInfo(nPoller, _T("cleanup"));
5039dede
AK
1212 if (pPollerNode != NULL)
1213 pPollerNode->DecRefCount();
1214
1215 if (dwOldFlags != m_dwFlags)
1216 {
1217 PostEvent(EVENT_NODE_FLAGS_CHANGED, m_dwId, "xx", dwOldFlags, m_dwFlags);
1218 LockData();
1219 Modify();
1220 UnlockData();
1221 }
1222
1223 CalculateCompoundStatus();
1224 m_tLastStatusPoll = time(NULL);
35f836fe
VK
1225 SendPollerMsg(dwRqId, _T("Finished status poll for node %s\r\n"), m_szName);
1226 SendPollerMsg(dwRqId, _T("Node status after poll is %s\r\n"), g_szStatusText[m_iStatus]);
5039dede
AK
1227 m_pPollRequestor = NULL;
1228 if (dwRqId == 0)
1229 m_dwDynamicFlags &= ~NDF_QUEUED_FOR_STATUS_POLL;
7c521895 1230 pollerUnlock();
35f836fe 1231 DbgPrintf(5, _T("Finished status poll for node %s (ID: %d)"), m_szName, m_dwId);
5039dede
AK
1232}
1233
1234
1235//
1f385e47
VK
1236// Check agent policy binding
1237// Intended to be called only from configuration poller
1238//
1239
1240void Node::checkAgentPolicyBinding(AgentConnection *conn)
1241{
1242 AgentPolicyInfo *ap;
1243 DWORD rcc = conn->getPolicyInventory(&ap);
1244 if (rcc == ERR_SUCCESS)
1245 {
1246 // Check for unbound but installed policies
1247 for(int i = 0; i < ap->getSize(); i++)
1248 {
1249 uuid_t guid;
1250 ap->getGuid(i, guid);
1251 NetObj *object = FindObjectByGUID(guid, -1);
1252 if ((object != NULL) && (!object->IsChild(m_dwId)))
1253 {
1254 object->AddChild(this);
1255 AddParent(object);
1256 DbgPrintf(5, _T("ConfPoll(%s): bound to policy object %s [%d]"), m_szName, object->Name(), object->Id());
1257 }
1258 }
1259
1260 // Check for bound but not installed policies
1261 LockParentList(FALSE);
1262 NetObj **unbindList = (NetObj **)malloc(sizeof(NetObj *) * m_dwParentCount);
1263 int unbindListSize = 0;
1264 for(DWORD i = 0; i < m_dwParentCount; i++)
1265 {
1266 if (IsAgentPolicyObject(m_pParentList[i]))
1267 {
1268 uuid_t guid1, guid2;
1269 int j;
1270
1271 m_pParentList[i]->getGuid(guid1);
1272 for(j = 0; j < ap->getSize(); j++)
1273 {
1274 ap->getGuid(j, guid2);
1275 if (!uuid_compare(guid1, guid2))
1276 break;
1277 }
1278 if (j == ap->getSize())
1279 unbindList[unbindListSize++] = m_pParentList[i];
1280 }
1281 }
1282 UnlockParentList();
1283
1284 for(int i = 0; i < unbindListSize; i++)
1285 {
1286 unbindList[i]->DeleteChild(this);
1287 DeleteParent(unbindList[i]);
1288 DbgPrintf(5, _T("ConfPoll(%s): unbound from policy object %s [%d]"), m_szName, unbindList[i]->Name(), unbindList[i]->Id());
1289 }
1290 safe_free(unbindList);
1291
1292 delete ap;
1293 }
1294 else
1295 {
1296 DbgPrintf(5, _T("ConfPoll(%s): AgentConnection::getPolicyInventory() failed: rcc=%d"), m_szName, rcc);
1297 }
1298}
1299
1300
1301//
0ecc2200
VK
1302// Walker callback for print MIB
1303//
1304
1305static DWORD PrintMIBWalkerCallback(DWORD version, SNMP_Variable *var, SNMP_Transport *transport, void *arg)
1306{
1307 (*((int *)arg))++;
1308 return SNMP_ERR_SUCCESS;
1309}
1310
1311
1312//
5039dede
AK
1313// Perform configuration poll on node
1314//
1315
58b3e451 1316void Node::configurationPoll(ClientSession *pSession, DWORD dwRqId,
5039dede
AK
1317 int nPoller, DWORD dwNetMask)
1318{
eec253a8 1319 DWORD dwOldFlags = m_dwFlags, dwAddr, rcc, dwNumParams;
5039dede 1320 NXC_AGENT_PARAM *pParamList;
5039dede 1321 AgentConnection *pAgentConn;
35f836fe 1322 TCHAR szBuffer[4096];
5039dede 1323 SNMP_Transport *pTransport;
5039dede
AK
1324 BOOL bHasChanges = FALSE;
1325
35f836fe 1326 SetPollerInfo(nPoller, _T("wait for lock"));
7c521895 1327 pollerLock();
5039dede
AK
1328 m_pPollRequestor = pSession;
1329 SendPollerMsg(dwRqId, _T("Starting configuration poll for node %s\r\n"), m_szName);
35f836fe 1330 DbgPrintf(4, _T("Starting configuration poll for node %s (ID: %d)"), m_szName, m_dwId);
5039dede
AK
1331
1332 // Check for forced capabilities recheck
1333 if (m_dwDynamicFlags & NDF_RECHECK_CAPABILITIES)
1334 {
1335 m_dwFlags &= ~(NF_IS_NATIVE_AGENT | NF_IS_SNMP | NF_IS_CPSNMP |
0ecc2200
VK
1336 NF_IS_BRIDGE | NF_IS_ROUTER | NF_IS_OSPF | NF_IS_PRINTER |
1337 NF_IS_CDP | NF_IS_LLDP | NF_IS_SONMP);
e4a64da2 1338 m_dwDynamicFlags &= ~NDF_CONFIGURATION_POLL_PASSED;
5039dede
AK
1339 m_szObjectId[0] = 0;
1340 m_szPlatformName[0] = 0;
1341 m_szAgentVersion[0] = 0;
0ecc2200
VK
1342 safe_free_and_null(m_sysDescription);
1343 safe_free_and_null(m_sysName);
1344 safe_free_and_null(m_lldpNodeId);
5039dede
AK
1345 }
1346
1347 // Check if node is marked as unreachable
1348 if ((m_dwDynamicFlags & NDF_UNREACHABLE) && !(m_dwDynamicFlags & NDF_RECHECK_CAPABILITIES))
1349 {
1350 SendPollerMsg(dwRqId, POLLER_WARNING _T("Node is marked as unreachable, configuration poll aborted\r\n"));
35f836fe 1351 DbgPrintf(4, _T("Node is marked as unreachable, configuration poll aborted"));
5039dede
AK
1352 m_tLastConfigurationPoll = time(NULL);
1353 }
1354 else
1355 {
1356 // Check node's capabilities
35f836fe 1357 SetPollerInfo(nPoller, _T("capability check"));
5039dede
AK
1358 SendPollerMsg(dwRqId, _T("Checking node's capabilities...\r\n"));
1359
1360 // ***** NetXMS agent check *****
35f836fe 1361 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent Flags={%08X} DynamicFlags={%08X}"), m_szName, m_dwFlags, m_dwDynamicFlags);
5039dede 1362 if ((!((m_dwFlags & NF_IS_NATIVE_AGENT) && (m_dwDynamicFlags & NDF_AGENT_UNREACHABLE))) &&
15d6f8c9 1363 (!(m_dwFlags & NF_DISABLE_NXCP)) && (m_dwIpAddr != 0))
5039dede
AK
1364 {
1365 SendPollerMsg(dwRqId, _T(" Checking NetXMS agent...\r\n"));
1366 pAgentConn = new AgentConnection(htonl(m_dwIpAddr), m_wAgentPort,
1367 m_wAuthMethod, m_szSharedSecret);
7c521895 1368 setAgentProxy(pAgentConn);
35f836fe 1369 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent - connecting"), m_szName);
7c521895 1370 if (pAgentConn->connect(g_pServerKey))
5039dede 1371 {
35f836fe 1372 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent - connected"), m_szName);
5039dede
AK
1373 LockData();
1374 m_dwFlags |= NF_IS_NATIVE_AGENT;
1375 if (m_dwDynamicFlags & NDF_AGENT_UNREACHABLE)
1376 {
1377 m_dwDynamicFlags &= ~NDF_AGENT_UNREACHABLE;
1378 PostEvent(EVENT_AGENT_OK, m_dwId, NULL);
1379 SendPollerMsg(dwRqId, POLLER_INFO _T(" Connectivity with NetXMS agent restored\r\n"));
1380 }
1f385e47
VK
1381 else
1382 {
1383 SendPollerMsg(dwRqId, POLLER_INFO _T(" NetXMS native agent is active\r\n"));
1384 }
5039dede
AK
1385 UnlockData();
1386
4687826e 1387 if (pAgentConn->getParameter(_T("Agent.Version"), MAX_AGENT_VERSION_LEN, szBuffer) == ERR_SUCCESS)
5039dede
AK
1388 {
1389 LockData();
35f836fe 1390 if (_tcscmp(m_szAgentVersion, szBuffer))
5039dede 1391 {
35f836fe 1392 _tcscpy(m_szAgentVersion, szBuffer);
5039dede
AK
1393 bHasChanges = TRUE;
1394 SendPollerMsg(dwRqId, _T(" NetXMS agent version changed to %s\r\n"), m_szAgentVersion);
1395 }
1396 UnlockData();
1397 }
1398
4687826e 1399 if (pAgentConn->getParameter(_T("System.PlatformName"), MAX_PLATFORM_NAME_LEN, szBuffer) == ERR_SUCCESS)
5039dede
AK
1400 {
1401 LockData();
35f836fe 1402 if (_tcscmp(m_szPlatformName, szBuffer))
5039dede 1403 {
35f836fe 1404 _tcscpy(m_szPlatformName, szBuffer);
5039dede
AK
1405 bHasChanges = TRUE;
1406 SendPollerMsg(dwRqId, _T(" Platform name changed to %s\r\n"), m_szPlatformName);
1407 }
1408 UnlockData();
1409 }
1410
1411 // Check IP forwarding status
4687826e 1412 if (pAgentConn->getParameter(_T("Net.IP.Forwarding"), 16, szBuffer) == ERR_SUCCESS)
5039dede
AK
1413 {
1414 if (_tcstoul(szBuffer, NULL, 10) != 0)
1415 m_dwFlags |= NF_IS_ROUTER;
1416 else
1417 m_dwFlags &= ~NF_IS_ROUTER;
1418 }
1419
1420 // Get uname
4687826e 1421 if (pAgentConn->getParameter(_T("System.Uname"), MAX_DB_STRING, szBuffer) == ERR_SUCCESS)
5039dede 1422 {
35f836fe
VK
1423 TranslateStr(szBuffer, _T("\r\n"), _T(" "));
1424 TranslateStr(szBuffer, _T("\n"), _T(" "));
1425 TranslateStr(szBuffer, _T("\r"), _T(" "));
5039dede 1426 LockData();
0ecc2200 1427 if ((m_sysDescription == NULL) || _tcscmp(m_sysDescription, szBuffer))
5039dede 1428 {
0ecc2200 1429 m_sysDescription = _tcsdup(szBuffer);
5039dede 1430 bHasChanges = TRUE;
0ecc2200 1431 SendPollerMsg(dwRqId, _T(" System description changed to %s\r\n"), m_sysDescription);
5039dede
AK
1432 }
1433 UnlockData();
1434 }
1435
4687826e 1436 rcc = pAgentConn->getSupportedParameters(&dwNumParams, &pParamList);
5039dede
AK
1437 if (rcc == ERR_SUCCESS)
1438 {
1439 LockData();
1440 safe_free(m_pParamList);
1441 m_dwNumParams = dwNumParams;
1442 m_pParamList = pParamList;
1443 UnlockData();
1444 }
1445 else
1446 {
1447 DbgPrintf(5, _T("ConfPoll(%s): AgentConnection::GetSupportedParameters() failed: rcc=%d"), m_szName, rcc);
1448 }
1449
1f385e47
VK
1450 checkAgentPolicyBinding(pAgentConn);
1451
7c521895 1452 pAgentConn->disconnect();
5039dede
AK
1453 }
1454 delete pAgentConn;
35f836fe 1455 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent - finished"), m_szName);
5039dede
AK
1456 }
1457
1458 // ***** SNMP check *****
1459 if ((!((m_dwFlags & NF_IS_SNMP) && (m_dwDynamicFlags & NDF_SNMP_UNREACHABLE))) &&
15d6f8c9 1460 (!(m_dwFlags & NF_DISABLE_SNMP)) && (m_dwIpAddr != 0))
5039dede
AK
1461 {
1462 SendPollerMsg(dwRqId, _T(" Checking SNMP...\r\n"));
35f836fe 1463 DbgPrintf(5, _T("ConfPoll(%s): calling SnmpCheckCommSettings()"), m_szName);
cd9f247e 1464 pTransport = createSnmpTransport();
f2ec1aae
VK
1465 if (pTransport == NULL)
1466 {
35f836fe 1467 DbgPrintf(5, _T("ConfPoll(%s): unable to create SNMP transport"), m_szName);
f2ec1aae
VK
1468 goto skip_snmp_checks;
1469 }
5039dede 1470
df8a4ca2
VK
1471 SNMP_SecurityContext *newCtx = SnmpCheckCommSettings(pTransport, &m_snmpVersion, m_snmpSecurity);
1472 if (newCtx != NULL)
5039dede
AK
1473 {
1474 DWORD dwNodeFlags, dwNodeType;
1475
1476 LockData();
df8a4ca2
VK
1477 delete m_snmpSecurity;
1478 m_snmpSecurity = newCtx;
5039dede
AK
1479 m_dwFlags |= NF_IS_SNMP;
1480 if (m_dwDynamicFlags & NDF_SNMP_UNREACHABLE)
1481 {
1482 m_dwDynamicFlags &= ~NDF_SNMP_UNREACHABLE;
1483 PostEvent(EVENT_SNMP_OK, m_dwId, NULL);
df8a4ca2 1484 SendPollerMsg(dwRqId, POLLER_INFO _T(" Connectivity with SNMP agent restored\r\n"));
5039dede
AK
1485 }
1486 UnlockData();
df8a4ca2
VK
1487 SendPollerMsg(dwRqId, _T(" SNMP agent is active (version %s)\r\n"),
1488 (m_snmpVersion == SNMP_VERSION_3) ? _T("3") : ((m_snmpVersion == SNMP_VERSION_2C) ? _T("2c") : _T("1")));
5039dede 1489
5d2c5741 1490 if (SnmpGet(m_snmpVersion, pTransport,
f42b8099 1491 _T(".1.3.6.1.2.1.1.2.0"), NULL, 0, szBuffer, 4096, SG_STRING_RESULT) == SNMP_ERR_SUCCESS)
5039dede
AK
1492 {
1493 LockData();
35f836fe 1494 if (_tcscmp(m_szObjectId, szBuffer))
5039dede
AK
1495 {
1496 nx_strncpy(m_szObjectId, szBuffer, MAX_OID_LEN * 4);
1497 bHasChanges = TRUE;
1498 }
1499 UnlockData();
1500 }
1501
1502 // Check node type
1503 LockData();
1504 dwNodeType = OidToType(m_szObjectId, &dwNodeFlags);
1505 if (m_dwNodeType != dwNodeType)
1506 {
1507 m_dwFlags |= dwNodeFlags;
1508 m_dwNodeType = dwNodeType;
1509 SendPollerMsg(dwRqId, POLLER_WARNING _T(" Node type has been changed to %d\r\n"), m_dwNodeType);
1510 bHasChanges = TRUE;
1511 }
1512 UnlockData();
1513
1514 // Check IP forwarding
35f836fe 1515 if (CheckSNMPIntegerValue(pTransport, _T(".1.3.6.1.2.1.4.1.0"), 1))
5039dede
AK
1516 {
1517 LockData();
1518 m_dwFlags |= NF_IS_ROUTER;
1519 UnlockData();
1520 }
1521 else
1522 {
1523 LockData();
1524 m_dwFlags &= ~NF_IS_ROUTER;
1525 UnlockData();
1526 }
1527
1528 // Check for bridge MIB support
0ecc2200 1529 if (SnmpGet(m_snmpVersion, pTransport, _T(".1.3.6.1.2.1.17.1.1.0"), NULL, 0, szBuffer, 4096, 0) == SNMP_ERR_SUCCESS)
5039dede
AK
1530 {
1531 LockData();
1532 m_dwFlags |= NF_IS_BRIDGE;
1533 UnlockData();
1534 }
1535 else
1536 {
1537 LockData();
1538 m_dwFlags &= ~NF_IS_BRIDGE;
1539 UnlockData();
1540 }
1541
0ecc2200
VK
1542 // Check for printer MIB support
1543 int count = 0;
1544 SnmpEnumerate(m_snmpVersion, pTransport, _T(".1.3.6.1.2.1.43.5.1.1.17"), PrintMIBWalkerCallback, &count, FALSE);
1545 if (count > 0)
1546 {
1547 LockData();
1548 m_dwFlags |= NF_IS_PRINTER;
1549 UnlockData();
1550 }
1551 else
1552 {
1553 LockData();
1554 m_dwFlags &= ~NF_IS_PRINTER;
1555 UnlockData();
1556 }
1557
5039dede 1558 // Check for CDP (Cisco Discovery Protocol) support
35f836fe 1559 if (CheckSNMPIntegerValue(pTransport, _T(".1.3.6.1.4.1.9.9.23.1.3.1.0"), 1))
5039dede
AK
1560 {
1561 LockData();
1562 m_dwFlags |= NF_IS_CDP;
1563 UnlockData();
1564 }
1565 else
1566 {
1567 LockData();
1568 m_dwFlags &= ~NF_IS_CDP;
1569 UnlockData();
1570 }
1571
ae2ef6f9 1572 // Check for NDP (Nortel Discovery Protocol) support
35f836fe 1573 if (CheckSNMPIntegerValue(pTransport, _T(".1.3.6.1.4.1.45.1.6.13.1.2.0"), 1))
5039dede
AK
1574 {
1575 LockData();
ae2ef6f9 1576 m_dwFlags |= NF_IS_NDP;
5039dede
AK
1577 UnlockData();
1578 }
1579 else
1580 {
1581 LockData();
ae2ef6f9 1582 m_dwFlags &= ~NF_IS_NDP;
5039dede
AK
1583 UnlockData();
1584 }
1585
1586 // Check for LLDP (Link Layer Discovery Protocol) support
0ecc2200 1587 if (SnmpGet(m_snmpVersion, pTransport, _T(".1.0.8802.1.1.2.1.3.2.0"), NULL, 0, szBuffer, 4096, 0) == SNMP_ERR_SUCCESS)
5039dede
AK
1588 {
1589 LockData();
1590 m_dwFlags |= NF_IS_LLDP;
1591 UnlockData();
0ecc2200
VK
1592
1593 if (SnmpGet(m_snmpVersion, pTransport, _T(".1.0.8802.1.1.2.1.3.1.0"), NULL, 0, szBuffer, 4096, SG_STRING_RESULT) == SNMP_ERR_SUCCESS)
1594 {
1595 _tcscat(szBuffer, _T("@"));
1596 int len = (int)_tcslen(szBuffer);
1597 if (SnmpGet(m_snmpVersion, pTransport, _T(".1.0.8802.1.1.2.1.3.2.0"), NULL, 0, &szBuffer[len], 4096 - len, SG_HSTRING_RESULT) == SNMP_ERR_SUCCESS)
1598 {
1599 LockData();
1600 if ((m_lldpNodeId == NULL) || _tcscmp(m_lldpNodeId, szBuffer))
1601 {
1602 safe_free(m_lldpNodeId);
1603 m_lldpNodeId = _tcsdup(szBuffer);
1604 bHasChanges = TRUE;
1605 SendPollerMsg(dwRqId, _T(" LLDP node ID changed to %s\r\n"), m_lldpNodeId);
1606 }
1607 UnlockData();
1608 }
1609 }
5039dede
AK
1610 }
1611 else
1612 {
1613 LockData();
1614 m_dwFlags &= ~NF_IS_LLDP;
1615 UnlockData();
1616 }
1617
1618 // Get system description
5d2c5741 1619 if (SnmpGet(m_snmpVersion, pTransport,
f42b8099 1620 _T(".1.3.6.1.2.1.1.1.0"), NULL, 0, szBuffer, MAX_DB_STRING, SG_STRING_RESULT) == SNMP_ERR_SUCCESS)
5039dede 1621 {
35f836fe
VK
1622 TranslateStr(szBuffer, _T("\r\n"), _T(" "));
1623 TranslateStr(szBuffer, _T("\n"), _T(" "));
1624 TranslateStr(szBuffer, _T("\r"), _T(" "));
5039dede 1625 LockData();
0ecc2200 1626 if ((m_sysDescription == NULL) || _tcscmp(m_sysDescription, szBuffer))
5039dede 1627 {
0ecc2200
VK
1628 safe_free(m_sysDescription);
1629 m_sysDescription = _tcsdup(szBuffer);
5039dede 1630 bHasChanges = TRUE;
0ecc2200 1631 SendPollerMsg(dwRqId, _T(" System description changed to %s\r\n"), m_sysDescription);
5039dede
AK
1632 }
1633 UnlockData();
1634 }
1635
1636 CheckOSPFSupport(pTransport);
0d75ea88
VK
1637
1638 // Get VRRP information
1639 VrrpInfo *vrrpInfo = GetVRRPInfo(this);
1640 if (vrrpInfo != NULL)
1641 {
1642 LockData();
1643 m_dwFlags |= NF_IS_VRRP;
1644 delete m_vrrpInfo;
1645 m_vrrpInfo = vrrpInfo;
1646 UnlockData();
1647 }
1648 else
1649 {
1650 LockData();
1651 m_dwFlags &= ~NF_IS_VRRP;
1652 UnlockData();
1653 }
5039dede
AK
1654 }
1655 else
1656 {
1657 // Check for CheckPoint SNMP agent on port 161
35f836fe 1658 DbgPrintf(5, _T("ConfPoll(%s): checking for CheckPoint SNMP"), m_szName);
5d2c5741 1659 if (SnmpGet(m_snmpVersion, pTransport,
f42b8099 1660 _T(".1.3.6.1.4.1.2620.1.1.10.0"), NULL, 0, szBuffer, 4096, 0) == SNMP_ERR_SUCCESS)
5039dede
AK
1661 {
1662 LockData();
35f836fe 1663 if (_tcscmp(m_szObjectId, _T(".1.3.6.1.4.1.2620.1.1")))
5039dede 1664 {
35f836fe 1665 nx_strncpy(m_szObjectId, _T(".1.3.6.1.4.1.2620.1.1"), MAX_OID_LEN * 4);
5039dede
AK
1666 bHasChanges = TRUE;
1667 }
1668
1669 m_dwFlags |= NF_IS_SNMP | NF_IS_ROUTER;
1670 m_dwDynamicFlags &= ~NDF_SNMP_UNREACHABLE;
1671 UnlockData();
1672 SendPollerMsg(dwRqId, POLLER_INFO _T(" CheckPoint SNMP agent on port 161 is active\r\n"));
1673 }
1674 }
1675 delete pTransport;
1676 }
1677
f2ec1aae 1678skip_snmp_checks:
5039dede 1679 // Check for CheckPoint SNMP agent on port 260
35f836fe 1680 DbgPrintf(5, _T("ConfPoll(%s): checking for CheckPoint SNMP on port 260"), m_szName);
15d6f8c9 1681 if (!((m_dwFlags & NF_IS_CPSNMP) && (m_dwDynamicFlags & NDF_CPSNMP_UNREACHABLE)) && (m_dwIpAddr != 0))
5039dede
AK
1682 {
1683 pTransport = new SNMP_UDPTransport;
c4366266 1684 ((SNMP_UDPTransport *)pTransport)->createUDPTransport(NULL, htonl(m_dwIpAddr), CHECKPOINT_SNMP_PORT);
5d2c5741 1685 if (SnmpGet(SNMP_VERSION_1, pTransport,
f42b8099 1686 _T(".1.3.6.1.4.1.2620.1.1.10.0"), NULL, 0, szBuffer, 4096, 0) == SNMP_ERR_SUCCESS)
5039dede
AK
1687 {
1688 LockData();
1689 m_dwFlags |= NF_IS_CPSNMP | NF_IS_ROUTER;
1690 m_dwDynamicFlags &= ~NDF_CPSNMP_UNREACHABLE;
1691 UnlockData();
1692 SendPollerMsg(dwRqId, POLLER_INFO _T(" CheckPoint SNMP agent on port 260 is active\r\n"));
1693 }
1694 delete pTransport;
1695 }
1696
1697 // Generate event if node flags has been changed
1698 if (dwOldFlags != m_dwFlags)
1699 {
1700 PostEvent(EVENT_NODE_FLAGS_CHANGED, m_dwId, "xx", dwOldFlags, m_dwFlags);
1701 bHasChanges = TRUE;
1702 }
1703
5039dede 1704 // Retrieve interface list
35f836fe 1705 SetPollerInfo(nPoller, _T("interface check"));
df8a4ca2 1706 SendPollerMsg(dwRqId, _T("Capability check finished\r\n"));
5039dede 1707
eec253a8
VK
1708 if (updateInterfaceConfiguration(dwRqId, dwNetMask))
1709 bHasChanges = TRUE;
5039dede
AK
1710
1711 m_tLastConfigurationPoll = time(NULL);
5039dede
AK
1712
1713 // Check node name
1714 SendPollerMsg(dwRqId, _T("Checking node name\r\n"));
1715 dwAddr = ntohl(_t_inet_addr(m_szName));
1716 if ((g_dwFlags & AF_RESOLVE_NODE_NAMES) &&
1717 (dwAddr != INADDR_NONE) &&
1718 (dwAddr != INADDR_ANY) &&
58b3e451 1719 isMyIP(dwAddr))
5039dede
AK
1720 {
1721 SendPollerMsg(dwRqId, _T("Node name is an IP address and need to be resolved\r\n"));
35f836fe 1722 SetPollerInfo(nPoller, _T("resolving name"));
5039dede
AK
1723 if (ResolveName(FALSE))
1724 {
1725 SendPollerMsg(dwRqId, POLLER_INFO _T("Node name resolved to %s\r\n"), m_szName);
1726 bHasChanges = TRUE;
1727 }
1728 else
1729 {
1730 SendPollerMsg(dwRqId, POLLER_WARNING _T("Node name cannot be resolved\r\n"));
1731 }
1732 }
1733 else
1734 {
1735 if (g_dwFlags & AF_SYNC_NODE_NAMES_WITH_DNS)
1736 {
1737 SendPollerMsg(dwRqId, _T("Syncing node name with DNS\r\n"));
35f836fe 1738 SetPollerInfo(nPoller, _T("resolving name"));
5039dede
AK
1739 if (ResolveName(TRUE))
1740 {
1741 SendPollerMsg(dwRqId, POLLER_INFO _T("Node name resolved to %s\r\n"), m_szName);
1742 bHasChanges = TRUE;
1743 }
1744 }
1745 else
1746 {
1747 SendPollerMsg(dwRqId, _T("Node name is OK\r\n"));
1748 }
1749 }
1750
4d0c32f3
VK
1751 // Apply templates
1752 ApplySystemTemplates();
1753 ApplyUserTemplates();
1754
7c521895 1755 updateContainerMembership();
4d0c32f3 1756
df8a4ca2
VK
1757 SendPollerMsg(dwRqId, _T("Finished configuration poll for node %s\r\n"), m_szName);
1758 SendPollerMsg(dwRqId, _T("Node configuration was%schanged after poll\r\n"), bHasChanges ? _T(" ") : _T(" not "));
e4a64da2
VK
1759
1760 m_dwDynamicFlags |= NDF_CONFIGURATION_POLL_PASSED;
4d0c32f3
VK
1761 }
1762
1763 // Finish configuration poll
35f836fe 1764 SetPollerInfo(nPoller, _T("cleanup"));
4d0c32f3
VK
1765 if (dwRqId == 0)
1766 m_dwDynamicFlags &= ~NDF_QUEUED_FOR_CONFIG_POLL;
1767 m_dwDynamicFlags &= ~NDF_RECHECK_CAPABILITIES;
7c521895 1768 pollerUnlock();
35f836fe 1769 DbgPrintf(4, _T("Finished configuration poll for node %s (ID: %d)"), m_szName, m_dwId);
4d0c32f3
VK
1770
1771 if (bHasChanges)
1772 {
1773 LockData();
1774 Modify();
1775 UnlockData();
1776 }
1777}
1778
1779
1780//
eec253a8
VK
1781// Update interface configuration
1782//
1783
1784BOOL Node::updateInterfaceConfiguration(DWORD dwRqId, DWORD dwNetMask)
1785{
98762401 1786 InterfaceList *pIfList;
eec253a8
VK
1787 Interface **ppDeleteList;
1788 int i, j, iDelCount;
1789 BOOL hasChanges = FALSE;
1790 Cluster *pCluster = getMyCluster();
1791
1792 SendPollerMsg(dwRqId, _T("Checking interface configuration...\r\n"));
1793 pIfList = getInterfaceList();
1794 if (pIfList != NULL)
1795 {
1796 // Remove cluster virtual interfaces from list
1797 if (pCluster != NULL)
1798 {
98762401 1799 for(i = 0; i < pIfList->getSize(); i++)
eec253a8 1800 {
98762401 1801 if (pCluster->isVirtualAddr(pIfList->get(i)->dwIpAddr))
eec253a8 1802 {
98762401 1803 pIfList->remove(i);
eec253a8
VK
1804 i--;
1805 }
1806 }
1807 }
1808
1809 // Find non-existing interfaces
1810 LockChildList(FALSE);
1811 ppDeleteList = (Interface **)malloc(sizeof(Interface *) * m_dwChildCount);
1812 for(i = 0, iDelCount = 0; i < (int)m_dwChildCount; i++)
1813 {
1814 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
1815 {
1816 Interface *pInterface = (Interface *)m_pChildList[i];
1817
1818 if (pInterface->getIfType() != IFTYPE_NETXMS_NAT_ADAPTER)
1819 {
98762401 1820 for(j = 0; j < pIfList->getSize(); j++)
eec253a8 1821 {
98762401
VK
1822 if ((pIfList->get(j)->dwIndex == pInterface->getIfIndex()) &&
1823 (pIfList->get(j)->dwIpAddr == pInterface->IpAddr()))
eec253a8
VK
1824 break;
1825 }
1826
98762401 1827 if (j == pIfList->getSize())
eec253a8
VK
1828 {
1829 // No such interface in current configuration, add it to delete list
1830 ppDeleteList[iDelCount++] = pInterface;
1831 }
1832 }
1833 }
1834 }
1835 UnlockChildList();
1836
1837 // Delete non-existent interfaces
1838 if (iDelCount > 0)
1839 {
1840 for(j = 0; j < iDelCount; j++)
1841 {
1842 SendPollerMsg(dwRqId, POLLER_WARNING _T(" Interface \"%s\" is no longer exist\r\n"),
1843 ppDeleteList[j]->Name());
1844 PostEvent(EVENT_INTERFACE_DELETED, m_dwId, "dsaa", ppDeleteList[j]->getIfIndex(),
1845 ppDeleteList[j]->Name(), ppDeleteList[j]->IpAddr(), ppDeleteList[j]->getIpNetMask());
1846 deleteInterface(ppDeleteList[j]);
1847 }
1848 hasChanges = TRUE;
1849 }
1850 safe_free(ppDeleteList);
1851
1852 // Add new interfaces and check configuration of existing
98762401 1853 for(j = 0; j < pIfList->getSize(); j++)
eec253a8 1854 {
98762401 1855 INTERFACE_INFO *ifInfo = pIfList->get(j);
eec253a8
VK
1856 BOOL bNewInterface = TRUE;
1857
1858 LockChildList(FALSE);
1859 for(i = 0; i < (int)m_dwChildCount; i++)
1860 {
1861 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
1862 {
1863 Interface *pInterface = (Interface *)m_pChildList[i];
1864
98762401
VK
1865 if ((ifInfo->dwIndex == pInterface->getIfIndex()) &&
1866 (ifInfo->dwIpAddr == pInterface->IpAddr()))
eec253a8
VK
1867 {
1868 // Existing interface, check configuration
98762401 1869 if (memcmp(ifInfo->bMacAddr, pInterface->getMacAddr(), MAC_ADDR_LENGTH))
eec253a8
VK
1870 {
1871 TCHAR szOldMac[16], szNewMac[16];
1872
1873 BinToStr((BYTE *)pInterface->getMacAddr(), MAC_ADDR_LENGTH, szOldMac);
98762401 1874 BinToStr(ifInfo->bMacAddr, MAC_ADDR_LENGTH, szNewMac);
eec253a8
VK
1875 PostEvent(EVENT_MAC_ADDR_CHANGED, m_dwId, "idsss",
1876 pInterface->Id(), pInterface->getIfIndex(),
1877 pInterface->Name(), szOldMac, szNewMac);
98762401 1878 pInterface->setMacAddr(ifInfo->bMacAddr);
eec253a8 1879 }
98762401 1880 if (_tcscmp(ifInfo->szName, pInterface->Name()))
eec253a8 1881 {
98762401 1882 pInterface->setName(ifInfo->szName);
eec253a8 1883 }
98762401 1884 if (ifInfo->dwBridgePortNumber != pInterface->getBridgePortNumber())
eec253a8 1885 {
98762401 1886 pInterface->setBridgePortNumber(ifInfo->dwBridgePortNumber);
eec253a8 1887 }
98762401 1888 if (ifInfo->dwSlotNumber != pInterface->getSlotNumber())
76f9abfd 1889 {
98762401 1890 pInterface->setSlotNumber(ifInfo->dwSlotNumber);
76f9abfd 1891 }
98762401 1892 if (ifInfo->dwPortNumber != pInterface->getPortNumber())
76f9abfd 1893 {
98762401 1894 pInterface->setPortNumber(ifInfo->dwPortNumber);
76f9abfd 1895 }
98762401 1896 if ((ifInfo->dwIpNetMask != 0) && (ifInfo->dwIpNetMask != pInterface->getIpNetMask()))
dfb38baf 1897 {
98762401 1898 pInterface->setIpNetMask(ifInfo->dwIpNetMask);
dfb38baf 1899 }
eec253a8
VK
1900 bNewInterface = FALSE;
1901 break;
1902 }
1903 }
1904 }
1905 UnlockChildList();
1906
1907 if (bNewInterface)
1908 {
1909 // New interface
98762401
VK
1910 SendPollerMsg(dwRqId, POLLER_INFO _T(" Found new interface \"%s\"\r\n"), ifInfo->szName);
1911 createNewInterface(ifInfo->dwIpAddr,
1912 ifInfo->dwIpNetMask,
1913 ifInfo->szName,
1914 ifInfo->dwIndex,
1915 ifInfo->dwType,
1916 ifInfo->bMacAddr,
1917 ifInfo->dwBridgePortNumber,
1918 ifInfo->dwSlotNumber,
1919 ifInfo->dwPortNumber);
eec253a8
VK
1920 hasChanges = TRUE;
1921 }
1922 }
1923
1924 // Check if address we are using to communicate with node
1925 // is configured on one of node's interfaces
98762401
VK
1926 for(i = 0; i < pIfList->getSize(); i++)
1927 if (pIfList->get(i)->dwIpAddr == m_dwIpAddr)
eec253a8
VK
1928 break;
1929
98762401 1930 if (i == (DWORD)pIfList->getSize())
eec253a8
VK
1931 {
1932 BOOL bCreate = TRUE;
1933
1934 // Node is behind NAT
1935 m_dwFlags |= NF_BEHIND_NAT;
1936
1937 // Check if we already have NAT interface
1938 LockChildList(FALSE);
1939 for(i = 0; i < (int)m_dwChildCount; i++)
1940 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
1941 {
1942 if (((Interface *)m_pChildList[i])->getIfType() == IFTYPE_NETXMS_NAT_ADAPTER)
1943 {
1944 bCreate = FALSE;
1945 break;
1946 }
1947 }
1948 UnlockChildList();
1949
1950 if (bCreate)
1951 {
1952 TCHAR szBuffer[MAX_OBJECT_NAME];
1953
1954 // Create pseudo interface for NAT
1955 ConfigReadStr(_T("NATAdapterName"), szBuffer, MAX_OBJECT_NAME, _T("NetXMS NAT Adapter"));
1956 createNewInterface(m_dwIpAddr, 0, szBuffer, 0x7FFFFFFF, IFTYPE_NETXMS_NAT_ADAPTER);
1957 hasChanges = TRUE;
1958 }
1959 }
1960 else
1961 {
1962 // Check if NF_BEHIND_NAT flag set incorrectly
1963 if (m_dwFlags & NF_BEHIND_NAT)
1964 {
1965 Interface *pIfNat;
1966
1967 // Remove NAT interface
1968 LockChildList(FALSE);
1969 for(i = 0, pIfNat = NULL; i < (int)m_dwChildCount; i++)
1970 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
1971 {
1972 if (((Interface *)m_pChildList[i])->getIfType() == IFTYPE_NETXMS_NAT_ADAPTER)
1973 {
1974 pIfNat = (Interface *)m_pChildList[i];
1975 break;
1976 }
1977 }
1978 UnlockChildList();
1979
1980 if (pIfNat != NULL)
1981 deleteInterface(pIfNat);
1982
1983 m_dwFlags &= ~NF_BEHIND_NAT;
1984 hasChanges = TRUE;
1985 }
1986 }
1987
1988 CheckSubnetBinding(pIfList);
1989
98762401 1990 delete pIfList;
eec253a8
VK
1991 }
1992 else /* pIfList == NULL */
1993 {
1994 Interface *pInterface;
1995 DWORD dwCount;
1996
1997 SendPollerMsg(dwRqId, POLLER_ERROR _T("Unable to get interface list from node\r\n"));
1998
1999 // Delete all existing interfaces in case of forced capability recheck
2000 if (m_dwDynamicFlags & NDF_RECHECK_CAPABILITIES)
2001 {
2002 LockChildList(FALSE);
2003 ppDeleteList = (Interface **)malloc(sizeof(Interface *) * m_dwChildCount);
2004 for(i = 0, iDelCount = 0; i < (int)m_dwChildCount; i++)
2005 {
2006 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
2007 ppDeleteList[iDelCount++] = (Interface *)m_pChildList[i];
2008 }
2009 UnlockChildList();
2010 for(j = 0; j < iDelCount; j++)
2011 {
2012 SendPollerMsg(dwRqId, POLLER_WARNING _T(" Interface \"%s\" is no longer exist\r\n"),
2013 ppDeleteList[j]->Name());
2014 PostEvent(EVENT_INTERFACE_DELETED, m_dwId, "dsaa", ppDeleteList[j]->getIfIndex(),
2015 ppDeleteList[j]->Name(), ppDeleteList[j]->IpAddr(), ppDeleteList[j]->getIpNetMask());
2016 deleteInterface(ppDeleteList[j]);
2017 }
2018 safe_free(ppDeleteList);
2019 }
2020
2021 // Check if we have pseudo-interface object
2022 dwCount = getInterfaceCount(&pInterface);
2023 if (dwCount == 1)
2024 {
2025 if (pInterface->isFake())
2026 {
2027 // Check if primary IP is different from interface's IP
2028 if (pInterface->IpAddr() != m_dwIpAddr)
2029 {
2030 deleteInterface(pInterface);
2031 if (m_dwIpAddr != 0)
2032 createNewInterface(m_dwIpAddr, dwNetMask);
2033 }
2034 }
2035 }
2036 else if (dwCount == 0)
2037 {
2038 // No interfaces at all, create pseudo-interface
2039 if (m_dwIpAddr != 0)
2040 createNewInterface(m_dwIpAddr, dwNetMask);
2041 }
2042 }
2043
2044 SendPollerMsg(dwRqId, _T("Interface configuration check finished\r\n"));
2045 return hasChanges;
2046}
2047
2048
2049//
4d0c32f3
VK
2050// Apply system templates
2051//
2052
2053void Node::ApplySystemTemplates()
2054{
2055 Template *pTemplate;
2056
2057 pTemplate = FindTemplateByName(_T("@System.Agent"));
2058 if (pTemplate != NULL)
2059 {
7c521895 2060 if (isNativeAgent())
5039dede 2061 {
4d0c32f3 2062 if (!pTemplate->IsChild(m_dwId))
5039dede 2063 {
4d0c32f3
VK
2064 DbgPrintf(4, _T("Node::ApplySystemTemplates(): applying template %d \"%s\" to node %d \"%s\""),
2065 pTemplate->Id(), pTemplate->Name(), m_dwId, m_szName);
2066 pTemplate->ApplyToNode(this);
5039dede 2067 }
4d0c32f3
VK
2068 }
2069 else
2070 {
2071 if (pTemplate->IsChild(m_dwId))
5039dede 2072 {
4d0c32f3
VK
2073 DbgPrintf(4, _T("Node::ApplySystemTemplates(): removing template %d \"%s\" from node %d \"%s\""),
2074 pTemplate->Id(), pTemplate->Name(), m_dwId, m_szName);
2075 pTemplate->DeleteChild(this);
2076 DeleteParent(pTemplate);
fb05c05b 2077 pTemplate->queueRemoveFromNode(m_dwId, TRUE);
5039dede
AK
2078 }
2079 }
4d0c32f3 2080 }
4d0c32f3
VK
2081}
2082
2083
2084//
2085// Apply user templates
2086//
2087
2088void Node::ApplyUserTemplates()
2089{
2090 DWORD i;
2091 Template *pTemplate;
2092
2093 if (g_pIndexById == NULL)
2094 return;
2095
2096 RWLockReadLock(g_rwlockIdIndex, INFINITE);
2097 for(i = 0; i < g_dwIdIndexSize; i++)
2098 {
2099 if ((((NetObj *)g_pIndexById[i].pObject)->Type() == OBJECT_TEMPLATE) &&
2100 (!((NetObj *)g_pIndexById[i].pObject)->IsDeleted()))
2101 {
2102 pTemplate = (Template *)g_pIndexById[i].pObject;
fb05c05b 2103 if (pTemplate->isApplicable(this))
5039dede
AK
2104 {
2105 if (!pTemplate->IsChild(m_dwId))
2106 {
4d0c32f3
VK
2107 DbgPrintf(4, _T("Node::ApplyUserTemplates(): applying template %d \"%s\" to node %d \"%s\""),
2108 pTemplate->Id(), pTemplate->Name(), m_dwId, m_szName);
5039dede
AK
2109 pTemplate->ApplyToNode(this);
2110 }
2111 }
2112 else
2113 {
fb05c05b 2114 if (pTemplate->isAutoApplyEnabled() && pTemplate->IsChild(m_dwId))
5039dede 2115 {
4d0c32f3
VK
2116 DbgPrintf(4, _T("Node::ApplyUserTemplates(): removing template %d \"%s\" from node %d \"%s\""),
2117 pTemplate->Id(), pTemplate->Name(), m_dwId, m_szName);
5039dede
AK
2118 pTemplate->DeleteChild(this);
2119 DeleteParent(pTemplate);
fb05c05b 2120 pTemplate->queueRemoveFromNode(m_dwId, TRUE);
5039dede
AK
2121 }
2122 }
4d0c32f3 2123 }
5039dede 2124 }
4d0c32f3
VK
2125 RWLockUnlock(g_rwlockIdIndex);
2126}
5039dede 2127
5039dede 2128
4d0c32f3
VK
2129//
2130// Update container membership
2131//
2132
7c521895 2133void Node::updateContainerMembership()
4d0c32f3
VK
2134{
2135 DWORD i;
2136 Container *pContainer;
2137
2138 if (g_pIndexById == NULL)
2139 return;
2140
2141 RWLockReadLock(g_rwlockIdIndex, INFINITE);
2142 for(i = 0; i < g_dwIdIndexSize; i++)
5039dede 2143 {
4d0c32f3
VK
2144 if ((((NetObj *)g_pIndexById[i].pObject)->Type() == OBJECT_CONTAINER) &&
2145 (!((NetObj *)g_pIndexById[i].pObject)->IsDeleted()))
2146 {
2147 pContainer = (Container *)g_pIndexById[i].pObject;
2148 if (pContainer->IsSuitableForNode(this))
2149 {
2150 if (!pContainer->IsChild(m_dwId))
2151 {
2152 DbgPrintf(4, _T("Node::UpdateContainerMembership(): binding node %d \"%s\" to container %d \"%s\""),
2153 m_dwId, m_szName, pContainer->Id(), pContainer->Name());
2154 pContainer->AddChild(this);
2155 AddParent(pContainer);
2156 }
2157 }
2158 else
2159 {
2160 if (pContainer->IsAutoBindEnabled() && pContainer->IsChild(m_dwId))
2161 {
2162 DbgPrintf(4, _T("Node::UpdateContainerMembership(): removing node %d \"%s\" from container %d \"%s\""),
2163 m_dwId, m_szName, pContainer->Id(), pContainer->Name());
2164 pContainer->DeleteChild(this);
2165 DeleteParent(pContainer);
2166 }
2167 }
2168 }
5039dede 2169 }
4d0c32f3 2170 RWLockUnlock(g_rwlockIdIndex);
5039dede
AK
2171}
2172
2173
2174//
2175// Connect to native agent
2176//
2177
a4569c4d 2178BOOL Node::connectToAgent(DWORD *error, DWORD *socketError)
5039dede
AK
2179{
2180 BOOL bRet;
2181
2182 // Create new agent connection object if needed
2183 if (m_pAgentConnection == NULL)
2184 m_pAgentConnection = new AgentConnectionEx(htonl(m_dwIpAddr), m_wAgentPort, m_wAuthMethod, m_szSharedSecret);
2185
2186 // Check if we already connected
45d84f8a 2187 if (m_pAgentConnection->nop() == ERR_SUCCESS)
5039dede
AK
2188 return TRUE;
2189
2190 // Close current connection or clean up after broken connection
7c521895
VK
2191 m_pAgentConnection->disconnect();
2192 m_pAgentConnection->setPort(m_wAgentPort);
35f836fe
VK
2193#ifdef UNICODE
2194 char mbSecret[MAX_SECRET_LENGTH];
2195 WideCharToMultiByte(CP_ACP, WC_DEFAULTCHAR | WC_COMPOSITECHECK, m_szSharedSecret, -1, mbSecret, MAX_SECRET_LENGTH, NULL, NULL);
2196 mbSecret[MAX_SECRET_LENGTH - 1] = 0;
2197 m_pAgentConnection->setAuthData(m_wAuthMethod, mbSecret);
2198#else
7c521895 2199 m_pAgentConnection->setAuthData(m_wAuthMethod, m_szSharedSecret);
35f836fe 2200#endif
7c521895 2201 setAgentProxy(m_pAgentConnection);
a4569c4d 2202 bRet = m_pAgentConnection->connect(g_pServerKey, FALSE, error, socketError);
5039dede
AK
2203 if (bRet)
2204 {
45d84f8a
VK
2205 m_pAgentConnection->setCommandTimeout(g_dwAgentCommandTimeout);
2206 m_pAgentConnection->enableTraps();
5039dede
AK
2207 }
2208 return bRet;
2209}
2210
2211
2212//
2213// Get item's value via SNMP
2214//
2215
35f836fe 2216DWORD Node::GetItemFromSNMP(WORD port, const TCHAR *szParam, DWORD dwBufSize, TCHAR *szBuffer)
5039dede
AK
2217{
2218 DWORD dwResult;
2219
2fd7144f 2220 if ((((m_dwDynamicFlags & NDF_SNMP_UNREACHABLE) || !(m_dwFlags & NF_IS_SNMP)) && (port == 0)) ||
6b2bb22c
VK
2221 (m_dwDynamicFlags & NDF_UNREACHABLE) ||
2222 (m_dwFlags & NF_DISABLE_SNMP))
5039dede
AK
2223 {
2224 dwResult = SNMP_ERR_COMM;
2225 }
2226 else
2227 {
2228 SNMP_Transport *pTransport;
2229
65e2005b 2230 pTransport = createSnmpTransport(port);
803d47be
VK
2231 if (pTransport != NULL)
2232 {
2233 dwResult = SnmpGet(m_snmpVersion, pTransport,
f42b8099 2234 szParam, NULL, 0, szBuffer, dwBufSize, SG_STRING_RESULT);
803d47be
VK
2235 delete pTransport;
2236 }
2237 else
2238 {
2239 dwResult = SNMP_ERR_COMM;
2240 }
5039dede 2241 }
35f836fe 2242 DbgPrintf(7, _T("Node(%s)->GetItemFromSNMP(%s): dwResult=%d"), m_szName, szParam, dwResult);
5039dede
AK
2243 return (dwResult == SNMP_ERR_SUCCESS) ? DCE_SUCCESS :
2244 ((dwResult == SNMP_ERR_NO_OBJECT) ? DCE_NOT_SUPPORTED : DCE_COMM_ERROR);
2245}
2246
2247
2248//
2249// Get item's value via SNMP from CheckPoint's agent
2250//
2251
35f836fe 2252DWORD Node::GetItemFromCheckPointSNMP(const TCHAR *szParam, DWORD dwBufSize, TCHAR *szBuffer)
5039dede
AK
2253{
2254 DWORD dwResult;
2255
2256 if ((m_dwDynamicFlags & NDF_CPSNMP_UNREACHABLE) ||
2257 (m_dwDynamicFlags & NDF_UNREACHABLE))
2258 {
2259 dwResult = SNMP_ERR_COMM;
2260 }
2261 else
2262 {
2263 SNMP_Transport *pTransport;
2264
2265 pTransport = new SNMP_UDPTransport;
c4366266 2266 ((SNMP_UDPTransport *)pTransport)->createUDPTransport(NULL, htonl(m_dwIpAddr), CHECKPOINT_SNMP_PORT);
f42b8099 2267 dwResult = SnmpGet(SNMP_VERSION_1, pTransport, szParam, NULL, 0, szBuffer, dwBufSize, SG_STRING_RESULT);
5039dede
AK
2268 delete pTransport;
2269 }
35f836fe 2270 DbgPrintf(7, _T("Node(%s)->GetItemFromCheckPointSNMP(%s): dwResult=%d"), m_szName, szParam, dwResult);
5039dede
AK
2271 return (dwResult == SNMP_ERR_SUCCESS) ? DCE_SUCCESS :
2272 ((dwResult == SNMP_ERR_NO_OBJECT) ? DCE_NOT_SUPPORTED : DCE_COMM_ERROR);
2273}
2274
2275
2276//
2277// Get item's value via native agent
2278//
2279
35f836fe 2280DWORD Node::GetItemFromAgent(const TCHAR *szParam, DWORD dwBufSize, TCHAR *szBuffer)
5039dede 2281{
912b994d 2282 DWORD dwError = ERR_NOT_CONNECTED, dwResult = DCE_COMM_ERROR;
5039dede
AK
2283 DWORD dwTries = 3;
2284
2285 if ((m_dwDynamicFlags & NDF_AGENT_UNREACHABLE) ||
2fd7144f
VK
2286 (m_dwDynamicFlags & NDF_UNREACHABLE) ||
2287 (m_dwFlags & NF_DISABLE_NXCP) ||
2288 !(m_dwFlags & NF_IS_NATIVE_AGENT))
5039dede
AK
2289 return DCE_COMM_ERROR;
2290
7c521895 2291 agentLock();
5039dede
AK
2292
2293 // Establish connection if needed
2294 if (m_pAgentConnection == NULL)
7c521895 2295 if (!connectToAgent())
5039dede
AK
2296 goto end_loop;
2297
2298 // Get parameter from agent
2299 while(dwTries-- > 0)
2300 {
4687826e 2301 dwError = m_pAgentConnection->getParameter(szParam, dwBufSize, szBuffer);
5039dede
AK
2302 switch(dwError)
2303 {
2304 case ERR_SUCCESS:
2305 dwResult = DCE_SUCCESS;
2306 goto end_loop;
2307 case ERR_UNKNOWN_PARAMETER:
2308 dwResult = DCE_NOT_SUPPORTED;
2309 goto end_loop;
2310 case ERR_NOT_CONNECTED:
2311 case ERR_CONNECTION_BROKEN:
7c521895 2312 if (!connectToAgent())
5039dede
AK
2313 goto end_loop;
2314 break;
2315 case ERR_REQUEST_TIMEOUT:
2316 // Reset connection to agent after timeout
2317 DbgPrintf(7, _T("Node(%s)->GetItemFromAgent(%s): timeout; resetting connection to agent..."), m_szName, szParam);
2318 delete_and_null(m_pAgentConnection);
7c521895 2319 if (!connectToAgent())
5039dede
AK
2320 goto end_loop;
2321 DbgPrintf(7, _T("Node(%s)->GetItemFromAgent(%s): connection to agent restored successfully"), m_szName, szParam);
2322 break;
2323 }
2324 }
2325
2326end_loop:
7c521895 2327 agentUnlock();
adac28be
VK
2328 DbgPrintf(7, _T("Node(%s)->GetItemFromAgent(%s): dwError=%d dwResult=%d"), m_szName, szParam, dwError, dwResult);
2329 return dwResult;
2330}
2331
2332
2333//
2334// Get table from agent
2335//
2336
2337DWORD Node::getTableFromAgent(const TCHAR *name, Table **table)
2338{
2339 DWORD dwError = ERR_NOT_CONNECTED, dwResult = DCE_COMM_ERROR;
2340 DWORD dwTries = 3;
2341
2342 *table = NULL;
2343
2344 if ((m_dwDynamicFlags & NDF_AGENT_UNREACHABLE) ||
2345 (m_dwDynamicFlags & NDF_UNREACHABLE) ||
2346 (m_dwFlags & NF_DISABLE_NXCP) ||
2347 !(m_dwFlags & NF_IS_NATIVE_AGENT))
2348 return DCE_COMM_ERROR;
2349
2350 agentLock();
2351
2352 // Establish connection if needed
2353 if (m_pAgentConnection == NULL)
2354 if (!connectToAgent())
2355 goto end_loop;
2356
2357 // Get parameter from agent
2358 while(dwTries-- > 0)
2359 {
2360 dwError = m_pAgentConnection->getTable(name, table);
2361 switch(dwError)
2362 {
2363 case ERR_SUCCESS:
2364 dwResult = DCE_SUCCESS;
2365 goto end_loop;
2366 case ERR_UNKNOWN_PARAMETER:
2367 dwResult = DCE_NOT_SUPPORTED;
2368 goto end_loop;
2369 case ERR_NOT_CONNECTED:
2370 case ERR_CONNECTION_BROKEN:
2371 if (!connectToAgent())
2372 goto end_loop;
2373 break;
2374 case ERR_REQUEST_TIMEOUT:
2375 // Reset connection to agent after timeout
2376 DbgPrintf(7, _T("Node(%s)->getTableFromAgent(%s): timeout; resetting connection to agent..."), m_szName, name);
2377 delete_and_null(m_pAgentConnection);
2378 if (!connectToAgent())
2379 goto end_loop;
2380 DbgPrintf(7, _T("Node(%s)->getTableFromAgent(%s): connection to agent restored successfully"), m_szName, name);
2381 break;
2382 }
2383 }
2384
2385end_loop:
2386 agentUnlock();
2387 DbgPrintf(7, _T("Node(%s)->getTableFromAgent(%s): dwError=%d dwResult=%d"), m_szName, name, dwError, dwResult);
5039dede
AK
2388 return dwResult;
2389}
2390
2391
2392//
2393// Get value for server's internal parameter
2394//
2395
35f836fe 2396DWORD Node::GetInternalItem(const TCHAR *szParam, DWORD dwBufSize, TCHAR *szBuffer)
5039dede
AK
2397{
2398 DWORD dwError = DCE_SUCCESS;
2399
35f836fe 2400 if (!_tcsicmp(szParam, _T("Status")))
5039dede 2401 {
35f836fe 2402 _sntprintf(szBuffer, dwBufSize, _T("%d"), m_iStatus);
5039dede 2403 }
35f836fe 2404 else if (!_tcsicmp(szParam, _T("Dummy")))
5039dede
AK
2405 {
2406 _tcscpy(szBuffer, _T("0"));
2407 }
35f836fe 2408 else if (!_tcsicmp(szParam, _T("AgentStatus")))
5039dede
AK
2409 {
2410 if (m_dwFlags & NF_IS_NATIVE_AGENT)
2411 {
35f836fe 2412 szBuffer[0] = (m_dwDynamicFlags & NDF_AGENT_UNREACHABLE) ? _T('1') : _T('0');
5039dede
AK
2413 szBuffer[1] = 0;
2414 }
2415 else
2416 {
2417 dwError = DCE_NOT_SUPPORTED;
2418 }
2419 }
35f836fe 2420 else if (MatchString(_T("ChildStatus(*)"), szParam, FALSE))
5039dede 2421 {
35f836fe 2422 TCHAR *pEnd, szArg[256];
5039dede
AK
2423 DWORD i, dwId;
2424 NetObj *pObject = NULL;
2425
35f836fe
VK
2426 AgentGetParameterArg(szParam, 1, szArg, 256);
2427 dwId = _tcstoul(szArg, &pEnd, 0);
5039dede
AK
2428 if (*pEnd != 0)
2429 {
2430 // Argument is object's name
2431 dwId = 0;
2432 }
2433
2434 // Find child object with requested ID or name
2435 LockChildList(FALSE);
2436 for(i = 0; i < m_dwChildCount; i++)
2437 {
35f836fe 2438 if (((dwId == 0) && (!_tcsicmp(m_pChildList[i]->Name(), szArg))) ||
5039dede
AK
2439 (dwId == m_pChildList[i]->Id()))
2440 {
2441 pObject = m_pChildList[i];
2442 break;
2443 }
2444 }
2445 UnlockChildList();
2446
2447 if (pObject != NULL)
2448 {
35f836fe 2449 _sntprintf(szBuffer, dwBufSize, _T("%d"), pObject->Status());
5039dede
AK
2450 }
2451 else
2452 {
2453 dwError = DCE_NOT_SUPPORTED;
2454 }
2455 }
35f836fe 2456 else if (MatchString(_T("ConditionStatus(*)"), szParam, FALSE))
5039dede 2457 {
35f836fe 2458 TCHAR *pEnd, szArg[256];
5039dede
AK
2459 DWORD dwId;
2460 NetObj *pObject = NULL;
2461
35f836fe
VK
2462 AgentGetParameterArg(szParam, 1, szArg, 256);
2463 dwId = _tcstoul(szArg, &pEnd, 0);
5039dede
AK
2464 if (*pEnd == 0)
2465 {
2466 pObject = FindObjectById(dwId);
2467 if (pObject != NULL)
2468 if (pObject->Type() != OBJECT_CONDITION)
2469 pObject = NULL;
2470 }
2471 else
2472 {
2473 // Argument is object's name
2474 pObject = FindObjectByName(szArg, OBJECT_CONDITION);
2475 }
2476
2477 if (pObject != NULL)
2478 {
2479 if (pObject->IsTrustedNode(m_dwId))
2480 {
35f836fe 2481 _sntprintf(szBuffer, dwBufSize, _T("%d"), pObject->Status());
5039dede
AK
2482 }
2483 else
2484 {
2485 dwError = DCE_NOT_SUPPORTED;
2486 }
2487 }
2488 else
2489 {
2490 dwError = DCE_NOT_SUPPORTED;
2491 }
2492 }
2493 else if (m_dwFlags & NF_IS_LOCAL_MGMT)
2494 {
35f836fe 2495 if (!_tcsicmp(szParam, _T("Server.AverageDCPollerQueueSize")))
5039dede 2496 {
35f836fe 2497 _sntprintf(szBuffer, dwBufSize, _T("%f"), g_dAvgPollerQueueSize);
5039dede 2498 }
35f836fe 2499 else if (!_tcsicmp(szParam, _T("Server.AverageDBWriterQueueSize")))
5039dede 2500 {
35f836fe 2501 _sntprintf(szBuffer, dwBufSize, _T("%f"), g_dAvgDBWriterQueueSize);
5039dede 2502 }
35f836fe 2503 else if (!_tcsicmp(szParam, _T("Server.AverageStatusPollerQueueSize")))
5039dede 2504 {
35f836fe 2505 _sntprintf(szBuffer, dwBufSize, _T("%f"), g_dAvgStatusPollerQueueSize);
5039dede 2506 }
35f836fe 2507 else if (!_tcsicmp(szParam, _T("Server.AverageConfigurationPollerQueueSize")))
5039dede 2508 {
35f836fe 2509 _sntprintf(szBuffer, dwBufSize, _T("%f"), g_dAvgConfigPollerQueueSize);
5039dede 2510 }
35f836fe 2511 else if (!_tcsicmp(szParam, _T("Server.AverageDCIQueuingTime")))
5039dede 2512 {
35f836fe 2513 _sntprintf(szBuffer, dwBufSize, _T("%u"), g_dwAvgDCIQueuingTime);
5039dede 2514 }
35f836fe 2515 else if (!_tcsicmp(szParam, _T("Server.TotalEventsProcessed")))
5039dede 2516 {
35f836fe 2517 _sntprintf(szBuffer, dwBufSize, INT64_FMT, g_totalEventsProcessed);
5039dede
AK
2518 }
2519 else
2520 {
2521 dwError = DCE_NOT_SUPPORTED;
2522 }
2523 }
2524 else
2525 {
2526 dwError = DCE_NOT_SUPPORTED;
2527 }
2528
2529 return dwError;
2530}
2531
2532
2533//
adac28be
VK
2534// Translate DCI error code into RCC
2535//
2536
2537static DWORD RCCFromDCIError(DWORD error)
2538{
3188558a 2539 switch(error)
adac28be
VK
2540 {
2541 case DCE_SUCCESS:
2542 return RCC_SUCCESS;
2543 case DCE_COMM_ERROR:
2544 return RCC_COMM_FAILURE;
2545 case DCE_NOT_SUPPORTED:
2546 return RCC_DCI_NOT_SUPPORTED;
2547 default:
2548 return RCC_SYSTEM_FAILURE;
2549 }
2550}
2551
2552
2553//
5039dede
AK
2554// Get item's value for client
2555//
2556
adac28be 2557DWORD Node::getItemForClient(int iOrigin, const TCHAR *pszParam, TCHAR *pszBuffer, DWORD dwBufSize)
5039dede
AK
2558{
2559 DWORD dwResult = 0, dwRetCode;
2560
2561 // Get data from node
2562 switch(iOrigin)
2563 {
2564 case DS_INTERNAL:
2565 dwRetCode = GetInternalItem(pszParam, dwBufSize, pszBuffer);
2566 break;
2567 case DS_NATIVE_AGENT:
2568 dwRetCode = GetItemFromAgent(pszParam, dwBufSize, pszBuffer);
2569 break;
2570 case DS_SNMP_AGENT:
65e2005b 2571 dwRetCode = GetItemFromSNMP(0, pszParam, dwBufSize, pszBuffer);
5039dede
AK
2572 break;
2573 case DS_CHECKPOINT_AGENT:
2574 dwRetCode = GetItemFromCheckPointSNMP(pszParam, dwBufSize, pszBuffer);
2575 break;
2576 default:
2577 dwResult = RCC_INVALID_ARGUMENT;
2578 break;
2579 }
2580
2581 // Translate return code to RCC
2582 if (dwResult != RCC_INVALID_ARGUMENT)
adac28be 2583 dwResult = RCCFromDCIError(dwRetCode);
5039dede
AK
2584
2585 return dwResult;
2586}
2587
2588
2589//
adac28be
VK
2590// Get table for client
2591//
2592
3188558a 2593DWORD Node::getTableForClient(const TCHAR *name, Table **table)
adac28be 2594{
3188558a 2595 DWORD dwRetCode = getTableFromAgent(name, table);
adac28be
VK
2596 return RCCFromDCIError(dwRetCode);
2597}
2598
2599
2600//
5039dede
AK
2601// Put items which requires polling into the queue
2602//
2603
7c521895 2604void Node::queueItemsForPolling(Queue *pPollerQueue)
5039dede
AK
2605{
2606 DWORD i;
2607 time_t currTime;
2608
2609 if ((m_iStatus == STATUS_UNMANAGED) ||
2610 (m_dwFlags & NF_DISABLE_DATA_COLLECT) ||
2611 (m_bIsDeleted))
2612 return; // Do not collect data for unmanaged nodes or if data collection is disabled
2613
2614 currTime = time(NULL);
2615
7c521895 2616 lockDciAccess();
5039dede
AK
2617 for(i = 0; i < m_dwNumItems; i++)
2618 {
fb05c05b 2619 if (m_ppItems[i]->isReadyForPolling(currTime))
5039dede 2620 {
fb05c05b 2621 m_ppItems[i]->setBusyFlag(TRUE);
5039dede
AK
2622 IncRefCount(); // Increment reference count for each queued DCI
2623 pPollerQueue->Put(m_ppItems[i]);
61b48d2f 2624 DbgPrintf(8, _T("Node(%s)->QueueItemsForPolling(): item %d \"%s\" added to queue"), m_szName, m_ppItems[i]->getId(), m_ppItems[i]->getName());
5039dede
AK
2625 }
2626 }
7c521895 2627 unlockDciAccess();
5039dede
AK
2628}
2629
2630
2631//
2632// Create CSCP message with object's data
2633//
2634
2635void Node::CreateMessage(CSCPMessage *pMsg)
2636{
2637 Template::CreateMessage(pMsg);
2638 pMsg->SetVariable(VID_FLAGS, m_dwFlags);
fa6173b9 2639 pMsg->SetVariable(VID_RUNTIME_FLAGS, m_dwDynamicFlags);
5039dede
AK
2640 pMsg->SetVariable(VID_AGENT_PORT, m_wAgentPort);
2641 pMsg->SetVariable(VID_AUTH_METHOD, m_wAuthMethod);
2642 pMsg->SetVariable(VID_SHARED_SECRET, m_szSharedSecret);
35f836fe
VK
2643 pMsg->SetVariableFromMBString(VID_SNMP_AUTH_OBJECT, m_snmpSecurity->getCommunity());
2644 pMsg->SetVariableFromMBString(VID_SNMP_AUTH_PASSWORD, m_snmpSecurity->getAuthPassword());
2645 pMsg->SetVariableFromMBString(VID_SNMP_PRIV_PASSWORD, m_snmpSecurity->getPrivPassword());
4b7d8903 2646 pMsg->SetVariable(VID_SNMP_USM_METHODS, (WORD)((WORD)m_snmpSecurity->getAuthMethod() | ((WORD)m_snmpSecurity->getPrivMethod() << 8)));
5039dede 2647 pMsg->SetVariable(VID_SNMP_OID, m_szObjectId);
65e2005b 2648 pMsg->SetVariable(VID_SNMP_PORT, m_wSNMPPort);
5039dede 2649 pMsg->SetVariable(VID_NODE_TYPE, m_dwNodeType);
5d2c5741 2650 pMsg->SetVariable(VID_SNMP_VERSION, (WORD)m_snmpVersion);
5039dede
AK
2651 pMsg->SetVariable(VID_AGENT_VERSION, m_szAgentVersion);
2652 pMsg->SetVariable(VID_PLATFORM_NAME, m_szPlatformName);
2653 pMsg->SetVariable(VID_POLLER_NODE_ID, m_dwPollerNode);
2654 pMsg->SetVariable(VID_ZONE_GUID, m_dwZoneGUID);
2655 pMsg->SetVariable(VID_PROXY_NODE, m_dwProxyNode);
2656 pMsg->SetVariable(VID_SNMP_PROXY, m_dwSNMPProxy);
2657 pMsg->SetVariable(VID_REQUIRED_POLLS, (WORD)m_iRequiredPollCount);
0ecc2200
VK
2658 pMsg->SetVariable(VID_SYS_NAME, CHECK_NULL_EX(m_sysName));
2659 pMsg->SetVariable(VID_SYS_DESCRIPTION, CHECK_NULL_EX(m_sysDescription));
2660 if (m_lldpNodeId != NULL)
2661 pMsg->SetVariable(VID_LLDP_NODE_ID, m_lldpNodeId);
5039dede 2662 pMsg->SetVariable(VID_USE_IFXTABLE, (WORD)m_nUseIfXTable);
0d75ea88
VK
2663 if (m_vrrpInfo != NULL)
2664 {
2665 pMsg->SetVariable(VID_VRRP_VERSION, (WORD)m_vrrpInfo->getVersion());
2666 pMsg->SetVariable(VID_VRRP_VR_COUNT, (WORD)m_vrrpInfo->getSize());
2667 }
5039dede
AK
2668}
2669
2670
2671//
2672// Modify object from message
2673//
2674
2675DWORD Node::ModifyFromMessage(CSCPMessage *pRequest, BOOL bAlreadyLocked)
2676{
2677 if (!bAlreadyLocked)
2678 LockData();
2679
2680 // Change primary IP address
2681 if (pRequest->IsVariableExist(VID_IP_ADDRESS))
2682 {
2683 DWORD i, dwIpAddr;
2684
2685 dwIpAddr = pRequest->GetVariableLong(VID_IP_ADDRESS);
2686
2687 // Check if received IP address is one of node's interface addresses
2688 LockChildList(FALSE);
2689 for(i = 0; i < m_dwChildCount; i++)
2690 if ((m_pChildList[i]->Type() == OBJECT_INTERFACE) &&
2691 (m_pChildList[i]->IpAddr() == dwIpAddr))
2692 break;
2693 UnlockChildList();
2694 if (i == m_dwChildCount)
2695 {
2696 UnlockData();
2697 return RCC_INVALID_IP_ADDR;
2698 }
2699
2700 UpdateNodeIndex(m_dwIpAddr, dwIpAddr, this);
2701 m_dwIpAddr = dwIpAddr;
2702 }
2703
2704 // Poller node ID
2705 if (pRequest->IsVariableExist(VID_POLLER_NODE_ID))
2706 {
2707 DWORD dwNodeId;
2708 NetObj *pObject;
2709
2710 dwNodeId = pRequest->GetVariableLong(VID_POLLER_NODE_ID);
2711 pObject = FindObjectById(dwNodeId);
2712
2713 // Check if received id is a valid node id
2714 if (pObject == NULL)
2715 {
2716 UnlockData();
2717 return RCC_INVALID_OBJECT_ID;
2718 }
2719 if (pObject->Type() != OBJECT_NODE)
2720 {
2721 UnlockData();
2722 return RCC_INVALID_OBJECT_ID;
2723 }
2724
2725 m_dwPollerNode = dwNodeId;
2726 }
2727
2728 // Change listen port of native agent
2729 if (pRequest->IsVariableExist(VID_AGENT_PORT))
2730 m_wAgentPort = pRequest->GetVariableShort(VID_AGENT_PORT);
2731
2732 // Change authentication method of native agent
2733 if (pRequest->IsVariableExist(VID_AUTH_METHOD))
2734 m_wAuthMethod = pRequest->GetVariableShort(VID_AUTH_METHOD);
2735
2736 // Change shared secret of native agent
2737 if (pRequest->IsVariableExist(VID_SHARED_SECRET))
2738 pRequest->GetVariableStr(VID_SHARED_SECRET, m_szSharedSecret, MAX_SECRET_LENGTH);
2739
2740 // Change SNMP protocol version
2741 if (pRequest->IsVariableExist(VID_SNMP_VERSION))
14b98b3a 2742 {
5d2c5741 2743 m_snmpVersion = pRequest->GetVariableShort(VID_SNMP_VERSION);
14b98b3a
VK
2744 m_snmpSecurity->setSecurityModel((m_snmpVersion == SNMP_VERSION_3) ? SNMP_SECURITY_MODEL_USM : SNMP_SECURITY_MODEL_V2C);
2745 }
5039dede 2746
65e2005b
VK
2747 // Change SNMP port
2748 if (pRequest->IsVariableExist(VID_SNMP_PORT))
2749 m_wSNMPPort = pRequest->GetVariableShort(VID_SNMP_PORT);
2750
4b7d8903
VK
2751 // Change SNMP authentication data
2752 if (pRequest->IsVariableExist(VID_SNMP_AUTH_OBJECT))
5d2c5741 2753 {
35f836fe
VK
2754 char mbBuffer[256];
2755
2756 pRequest->GetVariableStrA(VID_SNMP_AUTH_OBJECT, mbBuffer, 256);
2757 m_snmpSecurity->setAuthName(mbBuffer);
4b7d8903 2758
35f836fe
VK
2759 pRequest->GetVariableStrA(VID_SNMP_AUTH_PASSWORD, mbBuffer, 256);
2760 m_snmpSecurity->setAuthPassword(mbBuffer);
4b7d8903 2761
35f836fe
VK
2762 pRequest->GetVariableStrA(VID_SNMP_PRIV_PASSWORD, mbBuffer, 256);
2763 m_snmpSecurity->setPrivPassword(mbBuffer);
4b7d8903
VK
2764
2765 WORD methods = pRequest->GetVariableShort(VID_SNMP_USM_METHODS);
2766 m_snmpSecurity->setAuthMethod((int)(methods & 0xFF));
2767 m_snmpSecurity->setPrivMethod((int)(methods >> 8));
5d2c5741 2768 }
5039dede
AK
2769
2770 // Change proxy node
2771 if (pRequest->IsVariableExist(VID_PROXY_NODE))
2772 m_dwProxyNode = pRequest->GetVariableLong(VID_PROXY_NODE);
2773
2774 // Change SNMP proxy node
2775 if (pRequest->IsVariableExist(VID_SNMP_PROXY))
2776 m_dwSNMPProxy = pRequest->GetVariableLong(VID_SNMP_PROXY);
2777
2778 // Number of required polls
2779 if (pRequest->IsVariableExist(VID_REQUIRED_POLLS))
2780 m_iRequiredPollCount = (int)pRequest->GetVariableShort(VID_REQUIRED_POLLS);
2781
2782 // Enable/disable usage of ifXTable
2783 if (pRequest->IsVariableExist(VID_USE_IFXTABLE))
2784 m_nUseIfXTable = (BYTE)pRequest->GetVariableShort(VID_USE_IFXTABLE);
2785
2786 // Change flags
2787 if (pRequest->IsVariableExist(VID_FLAGS))
2788 {
2789 m_dwFlags &= NF_SYSTEM_FLAGS;
2790 m_dwFlags |= pRequest->GetVariableLong(VID_FLAGS) & NF_USER_FLAGS;
2791 }
2792
2793 return Template::ModifyFromMessage(pRequest, TRUE);
2794}
2795
2796
2797//
2798// Wakeup node using magic packet
2799//
2800
7c521895 2801DWORD Node::wakeUp()
5039dede
AK
2802{
2803 DWORD i, dwResult = RCC_NO_WOL_INTERFACES;
2804
2805 LockChildList(FALSE);
2806
2807 for(i = 0; i < m_dwChildCount; i++)
2808 if ((m_pChildList[i]->Type() == OBJECT_INTERFACE) &&
2809 (m_pChildList[i]->Status() != STATUS_UNMANAGED) &&
2810 (m_pChildList[i]->IpAddr() != 0))
2811 {
7c521895 2812 dwResult = ((Interface *)m_pChildList[i])->wakeUp();
5039dede
AK
2813 break;
2814 }
2815
2816 UnlockChildList();
2817 return dwResult;
2818}
2819
2820
2821//
2822// Get status of interface with given index from SNMP agent
2823//
2824
58b3e451 2825int Node::getInterfaceStatusFromSNMP(SNMP_Transport *pTransport, DWORD dwIndex)
5039dede 2826{
5d2c5741 2827 return SnmpGetInterfaceStatus(m_snmpVersion, pTransport, dwIndex);
5039dede
AK
2828}
2829
2830
2831//
2832// Get status of interface with given index from native agent
2833//
2834
58b3e451 2835int Node::getInterfaceStatusFromAgent(DWORD dwIndex)
5039dede 2836{
35f836fe 2837 TCHAR szParam[128], szBuffer[32];
5039dede
AK
2838 DWORD dwAdminStatus, dwLinkState;
2839 int iStatus;
2840
2841 // Get administrative status
35f836fe 2842 _sntprintf(szParam, 128, _T("Net.Interface.AdminStatus(%u)"), dwIndex);
5039dede
AK
2843 if (GetItemFromAgent(szParam, 32, szBuffer) == DCE_SUCCESS)
2844 {
35f836fe 2845 dwAdminStatus = _tcstoul(szBuffer, NULL, 0);
5039dede
AK
2846
2847 switch(dwAdminStatus)
2848 {
2849 case 3:
2850 iStatus = STATUS_TESTING;
2851 break;
2852 case 2:
2853 case 0: // Agents before 0.2.1 may return 0 instead of 2
2854 iStatus = STATUS_DISABLED;
2855 break;
2856 case 1: // Interface administratively up, check link state
35f836fe 2857 _sntprintf(szParam, 128, _T("Net.Interface.Link(%u)"), dwIndex);
5039dede
AK
2858 if (GetItemFromAgent(szParam, 32, szBuffer) == DCE_SUCCESS)
2859 {
35f836fe 2860 dwLinkState = _tcstoul(szBuffer, NULL, 0);
5039dede
AK
2861 iStatus = (dwLinkState == 0) ? STATUS_CRITICAL : STATUS_NORMAL;
2862 }
2863 else
2864 {
2865 iStatus = STATUS_UNKNOWN;
2866 }
2867 break;
2868 default:
2869 iStatus = STATUS_UNKNOWN;
2870 break;
2871 }
2872 }
2873 else
2874 {
2875 iStatus = STATUS_UNKNOWN;
2876 }
2877
2878 return iStatus;
2879}
2880
2881
2882//
2883// Put list of supported parameters into CSCP message
2884//
2885
2886void Node::WriteParamListToMessage(CSCPMessage *pMsg)
2887{
2888 DWORD i, dwId;
2889
2890 LockData();
2891 if (m_pParamList != NULL)
2892 {
2893 pMsg->SetVariable(VID_NUM_PARAMETERS, m_dwNumParams);
2894 for(i = 0, dwId = VID_PARAM_LIST_BASE; i < m_dwNumParams; i++)
2895 {
2896 pMsg->SetVariable(dwId++, m_pParamList[i].szName);
2897 pMsg->SetVariable(dwId++, m_pParamList[i].szDescription);
2898 pMsg->SetVariable(dwId++, (WORD)m_pParamList[i].iDataType);
2899 }
2900 DbgPrintf(6, _T("Node[%s]::WriteParamListToMessage(): sending %d parameters"), m_szName, m_dwNumParams);
2901 }
2902 else
2903 {
2904 DbgPrintf(6, _T("Node[%s]::WriteParamListToMessage(): m_pParamList == NULL"), m_szName);
2905 pMsg->SetVariable(VID_NUM_PARAMETERS, (DWORD)0);
2906 }
2907 UnlockData();
2908}
2909
2910
2911//
2912// Open list of supported parameters for reading
2913//
2914
2915void Node::OpenParamList(DWORD *pdwNumParams, NXC_AGENT_PARAM **ppParamList)
2916{
2917 LockData();
2918 *pdwNumParams = m_dwNumParams;
2919 *ppParamList = m_pParamList;
2920}
2921
2922
2923//
2924// Check status of network service
2925//
2926
2927DWORD Node::CheckNetworkService(DWORD *pdwStatus, DWORD dwIpAddr, int iServiceType,
2928 WORD wPort, WORD wProto, TCHAR *pszRequest,
2929 TCHAR *pszResponse)
2930{
2931 DWORD dwError = ERR_NOT_CONNECTED;
2932
2933 if ((m_dwFlags & NF_IS_NATIVE_AGENT) &&
2934 (!(m_dwDynamicFlags & NDF_AGENT_UNREACHABLE)) &&
2935 (!(m_dwDynamicFlags & NDF_UNREACHABLE)))
2936 {
2937 AgentConnection *pConn;
2938
cd9f247e 2939 pConn = createAgentConnection();
5039dede
AK
2940 if (pConn != NULL)
2941 {
4687826e 2942 dwError = pConn->checkNetworkService(pdwStatus, dwIpAddr, iServiceType,
5039dede 2943 wPort, wProto, pszRequest, pszResponse);
7c521895 2944 pConn->disconnect();
5039dede
AK
2945 delete pConn;
2946 }
2947 }
2948 return dwError;
2949}
2950
2951
2952//
2953// Handler for object deletion
2954//
2955
2956void Node::OnObjectDelete(DWORD dwObjectId)
2957{
f5d16551 2958 LockData();
5039dede
AK
2959 if (dwObjectId == m_dwPollerNode)
2960 {
2961 // If deleted object is our poller node, change it to default
5039dede
AK
2962 m_dwPollerNode = 0;
2963 Modify();
2964 DbgPrintf(3, _T("Node \"%s\": poller node %d deleted"), m_szName, dwObjectId);
2965 }
f5d16551 2966 UnlockData();
5039dede
AK
2967}
2968
2969
2970//
2971// Check node for OSPF support
2972//
2973
2974void Node::CheckOSPFSupport(SNMP_Transport *pTransport)
2975{
2976 LONG nAdminStatus;
2977
5d2c5741 2978 if (SnmpGet(m_snmpVersion, pTransport,
f42b8099 2979 _T(".1.3.6.1.2.1.14.1.2.0"), NULL, 0, &nAdminStatus, sizeof(LONG), 0) == SNMP_ERR_SUCCESS)
5039dede
AK
2980 {
2981 LockData();
2982 if (nAdminStatus)
2983 {
2984 m_dwFlags |= NF_IS_OSPF;
2985 }
2986 else
2987 {
2988 m_dwFlags &= ~NF_IS_OSPF;
2989 }
2990 UnlockData();
2991 }
2992}
2993
2994
2995//
2996// Create ready to use agent connection
2997//
2998
cd9f247e 2999AgentConnectionEx *Node::createAgentConnection()
5039dede 3000{
45d84f8a 3001 AgentConnectionEx *conn;
5039dede
AK
3002
3003 if ((!(m_dwFlags & NF_IS_NATIVE_AGENT)) ||
6b2bb22c 3004 (m_dwFlags & NF_DISABLE_NXCP) ||
5039dede
AK
3005 (m_dwDynamicFlags & NDF_AGENT_UNREACHABLE) ||
3006 (m_dwDynamicFlags & NDF_UNREACHABLE))
3007 return NULL;
3008
45d84f8a
VK
3009 conn = new AgentConnectionEx(htonl(m_dwIpAddr), m_wAgentPort,
3010 m_wAuthMethod, m_szSharedSecret);
7c521895
VK
3011 setAgentProxy(conn);
3012 if (!conn->connect(g_pServerKey))
5039dede 3013 {
45d84f8a
VK
3014 delete conn;
3015 conn = NULL;
5039dede 3016 }
45d84f8a 3017 return conn;
5039dede
AK
3018}
3019
3020
3021//
3022// Get last collected values of all DCIs
3023//
3024
7c521895 3025DWORD Node::getLastValues(CSCPMessage *pMsg)
5039dede
AK
3026{
3027 DWORD i, dwId, dwCount;
3028
7c521895 3029 lockDciAccess();
5039dede
AK
3030
3031 for(i = 0, dwId = VID_DCI_VALUES_BASE, dwCount = 0; i < m_dwNumItems; i++)
3032 {
fb05c05b 3033 if (_tcsnicmp(m_ppItems[i]->getDescription(), _T("@system."), 8))
5039dede 3034 {
fb05c05b 3035 m_ppItems[i]->getLastValue(pMsg, dwId);
5039dede
AK
3036 dwId += 10;
3037 dwCount++;
3038 }
3039 }
3040 pMsg->SetVariable(VID_NUM_ITEMS, dwCount);
3041
7c521895 3042 unlockDciAccess();
5039dede
AK
3043 return RCC_SUCCESS;
3044}
3045
3046
3047//
3048// Clean expired DCI data
3049//
3050
7c521895 3051void Node::cleanDCIData()
5039dede
AK
3052{
3053 DWORD i;
3054
7c521895 3055 lockDciAccess();
5039dede 3056 for(i = 0; i < m_dwNumItems; i++)
fb05c05b 3057 m_ppItems[i]->deleteExpiredData();
7c521895 3058 unlockDciAccess();
5039dede
AK
3059}
3060
3061
3062//
3063// Apply DCI from template
3064// pItem passed to this method should be a template's DCI
3065//
3066
a2069340 3067BOOL Node::applyTemplateItem(DWORD dwTemplateId, DCItem *pItem)
5039dede
AK
3068{
3069 BOOL bResult = TRUE;
3070 DWORD i;
3071 DCItem *pNewItem;
3072
a2069340 3073 lockDciAccess(); // write lock
5039dede 3074
35f836fe 3075 DbgPrintf(5, _T("Applying item \"%s\" to node \"%s\""), pItem->getName(), m_szName);
5039dede
AK
3076
3077 // Check if that template item exists
3078 for(i = 0; i < m_dwNumItems; i++)
fb05c05b
VK
3079 if ((m_ppItems[i]->getTemplateId() == dwTemplateId) &&
3080 (m_ppItems[i]->getTemplateItemId() == pItem->getId()))
5039dede
AK
3081 break; // Item with specified id already exist
3082
3083 if (i == m_dwNumItems)
3084 {
3085 // New item from template, just add it
3086 pNewItem = new DCItem(pItem);
fb05c05b
VK
3087 pNewItem->setTemplateId(dwTemplateId, pItem->getId());
3088 pNewItem->changeBinding(CreateUniqueId(IDG_ITEM), this, TRUE);
7c521895 3089 bResult = addItem(pNewItem, true);
5039dede
AK
3090 }
3091 else
3092 {
3093 // Update existing item
fb05c05b 3094 m_ppItems[i]->updateFromTemplate(pItem);
5039dede
AK
3095 }
3096
7c521895
VK
3097 unlockDciAccess();
3098
3099 if (bResult)
3100 {
3101 LockData();
3102 m_bIsModified = TRUE;
3103 UnlockData();
3104 }
5039dede
AK
3105 return bResult;
3106}
3107
3108
3109//
3110// Clean deleted template items from node's DCI list
3111// Arguments is template id and list of valid template item ids.
3112// all items related to given template and not presented in list should be deleted.
3113//
3114
a2069340 3115void Node::cleanDeletedTemplateItems(DWORD dwTemplateId, DWORD dwNumItems, DWORD *pdwItemList)
5039dede
AK
3116{
3117 DWORD i, j, dwNumDeleted, *pdwDeleteList;
3118
a2069340 3119 lockDciAccess(); // write lock
5039dede
AK
3120
3121 pdwDeleteList = (DWORD *)malloc(sizeof(DWORD) * m_dwNumItems);
3122 dwNumDeleted = 0;
3123
3124 for(i = 0; i < m_dwNumItems; i++)
fb05c05b 3125 if (m_ppItems[i]->getTemplateId() == dwTemplateId)
5039dede
AK
3126 {
3127 for(j = 0; j < dwNumItems; j++)
fb05c05b 3128 if (m_ppItems[i]->getTemplateItemId() == pdwItemList[j])
5039dede
AK
3129 break;
3130
3131 // Delete DCI if it's not in list
3132 if (j == dwNumItems)
fb05c05b 3133 pdwDeleteList[dwNumDeleted++] = m_ppItems[i]->getId();
5039dede
AK
3134 }
3135
3136 for(i = 0; i < dwNumDeleted; i++)
7c521895 3137 deleteItem(pdwDeleteList[i], false);
5039dede 3138
7c521895 3139 unlockDciAccess();
5039dede
AK
3140 free(pdwDeleteList);
3141}
3142
3143
3144//
3145// Unbind node from template, i.e either remove DCI association with template
3146// or remove these DCIs at all
3147//
3148
a2069340 3149void Node::unbindFromTemplate(DWORD dwTemplateId, BOOL bRemoveDCI)
5039dede 3150{
17b055bb 3151 DWORD i;
5039dede
AK
3152
3153 if (bRemoveDCI)
3154 {
17b055bb
VK
3155 DWORD *pdwDeleteList = (DWORD *)malloc(sizeof(DWORD) * m_dwNumItems);
3156 DWORD dwNumDeleted = 0;
5039dede 3157
a2069340 3158 lockDciAccess(); // write lock
5039dede
AK
3159
3160 for(i = 0; i < m_dwNumItems; i++)
fb05c05b 3161 if (m_ppItems[i]->getTemplateId() == dwTemplateId)
5039dede 3162 {
fb05c05b 3163 pdwDeleteList[dwNumDeleted++] = m_ppItems[i]->getId();
5039dede
AK
3164 }
3165
3166 for(i = 0; i < dwNumDeleted; i++)
7c521895 3167 deleteItem(pdwDeleteList[i], false);
5039dede 3168
7c521895 3169 unlockDciAccess();
17b055bb
VK
3170
3171 safe_free(pdwDeleteList);
5039dede
AK
3172 }
3173 else
3174 {
7c521895 3175 lockDciAccess();
5039dede
AK
3176
3177 for(i = 0; i < m_dwNumItems; i++)
fb05c05b 3178 if (m_ppItems[i]->getTemplateId() == dwTemplateId)
5039dede 3179 {
fb05c05b 3180 m_ppItems[i]->setTemplateId(0, 0);
5039dede
AK
3181 }
3182
7c521895 3183 unlockDciAccess();
5039dede
AK
3184 }
3185}
3186
3187
3188//
3189// Change node's IP address
3190//
3191
58b3e451 3192void Node::changeIPAddress(DWORD dwIpAddr)
5039dede
AK
3193{
3194 DWORD i;
3195
7c521895 3196 pollerLock();
5039dede
AK
3197
3198 LockData();
3199
3200 UpdateNodeIndex(m_dwIpAddr, dwIpAddr, this);
3201 m_dwIpAddr = dwIpAddr;
3202 m_dwDynamicFlags |= NDF_FORCE_CONFIGURATION_POLL | NDF_RECHECK_CAPABILITIES;
3203
3204 // Change status of node and all it's childs to UNKNOWN
3205 m_iStatus = STATUS_UNKNOWN;
3206 LockChildList(FALSE);
3207 for(i = 0; i < m_dwChildCount; i++)
3208 {
eec253a8 3209 m_pChildList[i]->resetStatus();
5039dede
AK
3210 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
3211 {
eec253a8 3212 if (((Interface *)m_pChildList[i])->isFake())
5039dede 3213 {
eec253a8 3214 ((Interface *)m_pChildList[i])->setIpAddr(dwIpAddr);
5039dede
AK
3215 }
3216 }
3217 }
3218 UnlockChildList();
3219
3220 Modify();
3221 UnlockData();
3222
7c521895 3223 agentLock();
5039dede 3224 delete_and_null(m_pAgentConnection);
7c521895 3225 agentUnlock();
5039dede 3226
7c521895 3227 pollerUnlock();
5039dede
AK
3228}
3229
3230
3231//
3232// Get number of interface objects and pointer to the last one
3233//
3234
7c521895 3235DWORD Node::getInterfaceCount(Interface **ppInterface)
5039dede
AK
3236{
3237 DWORD i, dwCount;
3238
3239 LockChildList(FALSE);
3240 for(i = 0, dwCount = 0; i < m_dwChildCount; i++)
3241 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
3242 {
3243 dwCount++;
3244 *ppInterface = (Interface *)m_pChildList[i];
3245 }
3246 UnlockChildList();
3247 return dwCount;
3248}
3249
3250
3251//
3252// Update cache for all DCI's
3253//
3254
a2069340 3255void Node::updateDciCache()
5039dede
AK
3256{
3257 DWORD i;
3258
a2069340 3259 lockDciAccess();
5039dede 3260 for(i = 0; i < m_dwNumItems; i++)
fb05c05b 3261 m_ppItems[i]->updateCacheSize();
a2069340 3262 unlockDciAccess();
5039dede
AK
3263}
3264
3265
3266//
3267// Get routing table from node
3268//
3269
58b3e451 3270ROUTING_TABLE *Node::getRoutingTable()
5039dede
AK
3271{
3272 ROUTING_TABLE *pRT = NULL;
3273
3274 if ((m_dwFlags & NF_IS_NATIVE_AGENT) && (!(m_dwFlags & NF_DISABLE_NXCP)))
3275 {
7c521895
VK
3276 agentLock();
3277 if (connectToAgent())
5039dede 3278 {
4687826e 3279 pRT = m_pAgentConnection->getRoutingTable();
5039dede 3280 }
7c521895 3281 agentUnlock();
5039dede
AK
3282 }
3283 if ((pRT == NULL) && (m_dwFlags & NF_IS_SNMP) && (!(m_dwFlags & NF_DISABLE_SNMP)))
3284 {
3285 SNMP_Transport *pTransport;
3286
cd9f247e 3287 pTransport = createSnmpTransport();
803d47be
VK
3288 if (pTransport != NULL)
3289 {
3290 pRT = SnmpGetRoutingTable(m_snmpVersion, pTransport);
3291 delete pTransport;
3292 }
5039dede
AK
3293 }
3294
3295 if (pRT != NULL)
3296 {
3297 SortRoutingTable(pRT);
3298 }
3299 return pRT;
3300}
3301
3302
3303//
3304// Get next hop for given destination address
3305//
3306
58b3e451 3307BOOL Node::getNextHop(DWORD dwSrcAddr, DWORD dwDestAddr, DWORD *pdwNextHop,
5039dede
AK
3308 DWORD *pdwIfIndex, BOOL *pbIsVPN)
3309{
3310 DWORD i;
3311 BOOL bResult = FALSE;
3312
3313 // Check VPN connectors
3314 LockChildList(FALSE);
3315 for(i = 0; i < m_dwChildCount; i++)
3316 if (m_pChildList[i]->Type() == OBJECT_VPNCONNECTOR)
3317 {
3318 if (((VPNConnector *)m_pChildList[i])->IsRemoteAddr(dwDestAddr) &&
3319 ((VPNConnector *)m_pChildList[i])->IsLocalAddr(dwSrcAddr))
3320 {
3321 *pdwNextHop = ((VPNConnector *)m_pChildList[i])->GetPeerGatewayAddr();
3322 *pdwIfIndex = m_pChildList[i]->Id();
3323 *pbIsVPN = TRUE;
3324 bResult = TRUE;
3325 break;
3326 }
3327 }
3328 UnlockChildList();
3329
3330 // Check routing table
3331 if (!bResult)
3332 {
7c521895 3333 routingTableLock();
5039dede
AK
3334 if (m_pRoutingTable != NULL)
3335 {
3336 for(i = 0; i < (DWORD)m_pRoutingTable->iNumEntries; i++)
3337 if ((dwDestAddr & m_pRoutingTable->pRoutes[i].dwDestMask) == m_pRoutingTable->pRoutes[i].dwDestAddr)
3338 {
3339 *pdwNextHop = m_pRoutingTable->pRoutes[i].dwNextHop;
3340 *pdwIfIndex = m_pRoutingTable->pRoutes[i].dwIfIndex;
3341 *pbIsVPN = FALSE;
3342 bResult = TRUE;
3343 break;
3344 }
3345 }
7c521895 3346 routingTableUnlock();
5039dede
AK
3347 }
3348
3349 return bResult;
3350}
3351
3352
3353//
3354// Update cached routing table
3355//
3356
58b3e451 3357void Node::updateRoutingTable()
5039dede
AK
3358{
3359 ROUTING_TABLE *pRT;
3360
58b3e451 3361 pRT = getRoutingTable();
5039dede
AK
3362 if (pRT != NULL)
3363 {
7c521895 3364 routingTableLock();
5039dede
AK
3365 DestroyRoutingTable(m_pRoutingTable);
3366 m_pRoutingTable = pRT;
7c521895 3367 routingTableUnlock();
5039dede
AK
3368 }
3369 m_tLastRTUpdate = time(NULL);
3370 m_dwDynamicFlags &= ~NDF_QUEUED_FOR_ROUTE_POLL;
3371}
3372
3373
3374//
3375// Call SNMP Enumerate with node's SNMP parameters
3376//
3377
35f836fe 3378DWORD Node::CallSnmpEnumerate(const TCHAR *pszRootOid,
5d2c5741 3379 DWORD (* pHandler)(DWORD, SNMP_Variable *, SNMP_Transport *, void *),
5039dede
AK
3380 void *pArg)
3381{
3382 if ((m_dwFlags & NF_IS_SNMP) &&
3383 (!(m_dwDynamicFlags & NDF_SNMP_UNREACHABLE)) &&
3384 (!(m_dwDynamicFlags & NDF_UNREACHABLE)))
3385 {
3386 SNMP_Transport *pTransport;
3387 DWORD dwResult;
3388
cd9f247e 3389 pTransport = createSnmpTransport();
803d47be
VK
3390 if (pTransport != NULL)
3391 {
3392 dwResult = SnmpEnumerate(m_snmpVersion, pTransport,
3393 pszRootOid, pHandler, pArg, FALSE);
3394 delete pTransport;
3395 }
3396 else
3397 {
3398 dwResult = SNMP_ERR_COMM;
3399 }
5039dede
AK
3400 return dwResult;
3401 }
3402 else
3403 {
3404 return SNMP_ERR_COMM;
3405 }
3406}
3407
3408
3409//
3410// Set proxy information for agent's connection
3411//
3412
7c521895 3413void Node::setAgentProxy(AgentConnection *pConn)
5039dede
AK
3414{
3415 if (m_dwProxyNode != 0)
3416 {
3417 Node *pNode;
3418
3419 pNode = (Node *)FindObjectById(m_dwProxyNode);
3420 if (pNode != NULL)
3421 {
7c521895 3422 pConn->setProxy(htonl(pNode->m_dwIpAddr), pNode->m_wAgentPort,
5039dede
AK
3423 pNode->m_wAuthMethod, pNode->m_szSharedSecret);
3424 }
3425 }
3426}
3427
3428
3429//
3430// Prepare node object for deletion
3431//
3432
3433void Node::PrepareForDeletion(void)
3434{
3435 // Prevent node from being queued for polling
3436 LockData();
3437 m_dwDynamicFlags |= NDF_POLLING_DISABLED;
3438 UnlockData();
3439
3440 // Wait for all pending polls
3441 while(1)
3442 {
3443 LockData();
3444 if ((m_dwDynamicFlags &
3445 (NDF_QUEUED_FOR_STATUS_POLL | NDF_QUEUED_FOR_CONFIG_POLL |
3446 NDF_QUEUED_FOR_DISCOVERY_POLL | NDF_QUEUED_FOR_ROUTE_POLL)) == 0)
3447 {
3448 UnlockData();
3449 break;
3450 }
3451 UnlockData();
3452 ThreadSleepMs(100);
3453 }
3454 Template::PrepareForDeletion();
3455}
3456
3457
3458//
3459// Check if specified SNMP variable set to specified value.
3460// If variable doesn't exist at all, will return FALSE
3461//
3462
35f836fe