fixed potential NULL pointer access
[public/netxms.git] / src / server / core / np.cpp
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
496390c2 3** Copyright (C) 2003-2013 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: np.cpp
20**
21**/
22
23#include "nxcore.h"
24
56e92fc9
VK
25/**
26 * Externals
27 */
5039dede
AK
28extern Queue g_nodePollerQueue;
29
56e92fc9
VK
30/**
31 * Discovery class
32 */
5039dede
AK
33class NXSL_DiscoveryClass : public NXSL_Class
34{
35public:
36 NXSL_DiscoveryClass();
37
bb51576f 38 virtual NXSL_Value *getAttr(NXSL_Object *pObject, const TCHAR *pszAttr);
5039dede
AK
39};
40
56e92fc9
VK
41/**
42 * Implementation of discovery class
43 */
7c80f505 44NXSL_DiscoveryClass::NXSL_DiscoveryClass() : NXSL_Class()
5039dede 45{
35f836fe 46 _tcscpy(m_szName, _T("NewNode"));
5039dede
AK
47}
48
bb51576f 49NXSL_Value *NXSL_DiscoveryClass::getAttr(NXSL_Object *pObject, const TCHAR *pszAttr)
5039dede
AK
50{
51 DISCOVERY_FILTER_DATA *pData;
52 NXSL_Value *pValue = NULL;
35f836fe 53 TCHAR szBuffer[256];
5039dede 54
bb5365ed 55 pData = (DISCOVERY_FILTER_DATA *)pObject->getData();
35f836fe 56 if (!_tcscmp(pszAttr, _T("ipAddr")))
5039dede
AK
57 {
58 IpToStr(pData->dwIpAddr, szBuffer);
59 pValue = new NXSL_Value(szBuffer);
60 }
35f836fe 61 else if (!_tcscmp(pszAttr, _T("netMask")))
5039dede
AK
62 {
63 IpToStr(pData->dwNetMask, szBuffer);
64 pValue = new NXSL_Value(szBuffer);
65 }
35f836fe 66 else if (!_tcscmp(pszAttr, _T("subnet")))
5039dede
AK
67 {
68 IpToStr(pData->dwSubnetAddr, szBuffer);
69 pValue = new NXSL_Value(szBuffer);
70 }
35f836fe 71 else if (!_tcscmp(pszAttr, _T("isAgent")))
5039dede
AK
72 {
73 pValue = new NXSL_Value((LONG)((pData->dwFlags & NNF_IS_AGENT) ? 1 : 0));
74 }
35f836fe 75 else if (!_tcscmp(pszAttr, _T("isSNMP")))
5039dede
AK
76 {
77 pValue = new NXSL_Value((LONG)((pData->dwFlags & NNF_IS_SNMP) ? 1 : 0));
78 }
35f836fe 79 else if (!_tcscmp(pszAttr, _T("isBridge")))
5039dede
AK
80 {
81 pValue = new NXSL_Value((LONG)((pData->dwFlags & NNF_IS_BRIDGE) ? 1 : 0));
82 }
35f836fe 83 else if (!_tcscmp(pszAttr, _T("isRouter")))
5039dede
AK
84 {
85 pValue = new NXSL_Value((LONG)((pData->dwFlags & NNF_IS_ROUTER) ? 1 : 0));
86 }
35f836fe 87 else if (!_tcscmp(pszAttr, _T("isPrinter")))
5039dede
AK
88 {
89 pValue = new NXSL_Value((LONG)((pData->dwFlags & NNF_IS_PRINTER) ? 1 : 0));
90 }
35f836fe 91 else if (!_tcscmp(pszAttr, _T("isCDP")))
5039dede
AK
92 {
93 pValue = new NXSL_Value((LONG)((pData->dwFlags & NNF_IS_CDP) ? 1 : 0));
94 }
35f836fe 95 else if (!_tcscmp(pszAttr, _T("isSONMP")))
5039dede
AK
96 {
97 pValue = new NXSL_Value((LONG)((pData->dwFlags & NNF_IS_SONMP) ? 1 : 0));
98 }
35f836fe 99 else if (!_tcscmp(pszAttr, _T("isLLDP")))
5039dede
AK
100 {
101 pValue = new NXSL_Value((LONG)((pData->dwFlags & NNF_IS_LLDP) ? 1 : 0));
102 }
35f836fe 103 else if (!_tcscmp(pszAttr, _T("snmpVersion")))
5039dede
AK
104 {
105 pValue = new NXSL_Value((LONG)pData->nSNMPVersion);
106 }
35f836fe 107 else if (!_tcscmp(pszAttr, _T("snmpOID")))
5039dede
AK
108 {
109 pValue = new NXSL_Value(pData->szObjectId);
110 }
35f836fe 111 else if (!_tcscmp(pszAttr, _T("agentVersion")))
5039dede
AK
112 {
113 pValue = new NXSL_Value(pData->szAgentVersion);
114 }
35f836fe 115 else if (!_tcscmp(pszAttr, _T("platformName")))
5039dede
AK
116 {
117 pValue = new NXSL_Value(pData->szPlatform);
118 }
119 return pValue;
120}
121
22d657d2
VK
122/**
123 * Discovery class object
124 */
5039dede
AK
125static NXSL_DiscoveryClass m_nxslDiscoveryClass;
126
22d657d2
VK
127/**
128 * Poll new node for configuration
129 * Returns pointer to new node object on success or NULL on failure
130 *
131 * @param dwIpAddr IP address of new node
132 * @param dwNetMask IP network mask or 0 if not known
133 * @param dwCreationFlags
134 * @param agentPort port number of NetXMS agent
135 * @param snmpPort port number of SNMP agent
136 * @param pszName name for new node, can be NULL
137 * @param dwProxyNode agent proxy node ID or 0 to use direct communications
138 * @param dwSNMPProxy SNMP proxy node ID or 0 to use direct communications
139 * @param pCluster pointer to parent cluster object or NULL
140 * @param zoneId zone ID
141 * @param doConfPoll if set to true, Node::configurationPoll will be called before exit
142 * @param discoveredNode must be set to true if node being added automatically by discovery thread
143 */
967893bb
VK
144Node *PollNewNode(UINT32 dwIpAddr, UINT32 dwNetMask, UINT32 dwCreationFlags,
145 WORD agentPort, WORD snmpPort, const TCHAR *pszName, UINT32 dwProxyNode, UINT32 dwSNMPProxy,
146 Cluster *pCluster, UINT32 zoneId, bool doConfPoll, bool discoveredNode)
5039dede
AK
147{
148 Node *pNode;
35f836fe 149 TCHAR szIpAddr1[32], szIpAddr2[32];
967893bb 150 UINT32 dwFlags = 0;
5039dede 151
dc25b21c 152 DbgPrintf(4, _T("PollNode(%s,%s) zone %d"), IpToStr(dwIpAddr, szIpAddr1), IpToStr(dwNetMask, szIpAddr2), (int)zoneId);
5039dede 153 // Check for node existence
89135050
VK
154 if ((FindNodeByIP(zoneId, dwIpAddr) != NULL) ||
155 (FindSubnetByIP(zoneId, dwIpAddr) != NULL))
5039dede 156 {
35f836fe 157 DbgPrintf(4, _T("PollNode: Node %s already exist in database"),
5039dede
AK
158 IpToStr(dwIpAddr, szIpAddr1));
159 return NULL;
160 }
161
162 if (dwCreationFlags & NXC_NCF_DISABLE_ICMP)
163 dwFlags |= NF_DISABLE_ICMP;
164 if (dwCreationFlags & NXC_NCF_DISABLE_SNMP)
165 dwFlags |= NF_DISABLE_SNMP;
166 if (dwCreationFlags & NXC_NCF_DISABLE_NXCP)
167 dwFlags |= NF_DISABLE_NXCP;
dc25b21c 168 pNode = new Node(dwIpAddr, dwFlags, dwProxyNode, dwSNMPProxy, zoneId);
f1b6417b
VK
169 if (agentPort != 0)
170 pNode->setAgentPort(agentPort);
171 if (snmpPort != 0)
172 pNode->setSnmpPort(snmpPort);
5039dede
AK
173 NetObjInsert(pNode, TRUE);
174 if (pszName != NULL)
eec253a8 175 pNode->setName(pszName);
5039dede 176
a749a70a
VK
177 // Use DNS name as primary name if required
178 if (discoveredNode && ConfigReadInt(_T("UseDNSNameForDiscoveredNodes"), 0))
179 {
967893bb 180 UINT32 ip = htonl(dwIpAddr);
a749a70a
VK
181 struct hostent *hs = gethostbyaddr((char *)&ip, 4, AF_INET);
182 if (hs != NULL)
183 {
184 TCHAR dnsName[MAX_DNS_NAME];
185#ifdef UNICODE
186 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, hs->h_name, -1, dnsName, MAX_DNS_NAME);
187 dnsName[MAX_DNS_NAME - 1] = 0;
188#else
189 nx_strncpy(dnsName, hs->h_name, MAX_DNS_NAME);
190#endif
191 if (ntohl(ResolveHostName(dnsName)) == dwIpAddr)
192 {
193 // We have valid DNS name which resolves back to node's IP address, use it as primary name
194 pNode->setPrimaryName(dnsName);
195 DbgPrintf(4, _T("PollNode: Using DNS name %s as primary name for node %s"), dnsName, IpToStr(dwIpAddr, szIpAddr1));
196 }
197 }
198 }
199
5039dede
AK
200 // Bind node to cluster before first configuration poll
201 if (pCluster != NULL)
202 {
6fd6de0a 203 pCluster->applyToTarget(pNode);
5039dede
AK
204 }
205
206 if (dwCreationFlags & NXC_NCF_CREATE_UNMANAGED)
207 {
eec253a8 208 pNode->setMgmtStatus(FALSE);
5039dede 209 }
5039dede
AK
210
211 // Add default DCIs
f05a8a45
VK
212 pNode->addDCObject(new DCItem(CreateUniqueId(IDG_ITEM), _T("Status"), DS_INTERNAL, DCI_DT_INT,
213 ConfigReadInt(_T("DefaultDCIPollingInterval"), 60),
214 ConfigReadInt(_T("DefaultDCIRetentionTime"), 30), pNode));
588f7351
VK
215
216 if (doConfPoll)
217 pNode->configurationPoll(NULL, 0, -1, dwNetMask);
218
219 pNode->unhide();
220 PostEvent(EVENT_NODE_ADDED, pNode->Id(), NULL);
221
5039dede
AK
222 return pNode;
223}
224
22d657d2
VK
225/**
226 * Find existing node by MAC address to detect IP address change for already known node.
227 *
228 * @param dwIpAddr new (discovered) IP address
229 * @param dwNetMask network mask for new address
230 * @param dwZoneID zone ID
231 * @param bMacAddr MAC address of discovered node, or NULL if not known
232 *
233 * @return pointer to existing interface object with given MAC address or NULL if no such interface found
234 */
967893bb 235static Interface *GetOldNodeWithNewIP(UINT32 dwIpAddr, UINT32 dwNetMask, UINT32 dwZoneId, BYTE *bMacAddr)
baa5324c
AK
236{
237 Subnet *subnet;
238 BYTE nodeMacAddr[MAC_ADDR_LENGTH];
239 TCHAR szIpAddr[16], szMacAddr[20];
240
241 IpToStr(dwIpAddr, szIpAddr);
242 MACToStr(bMacAddr, szMacAddr);
243 DbgPrintf(6, _T("GetOldNodeWithNewIP: ip=%s mac=%s"), szIpAddr, szMacAddr);
244
245 if (bMacAddr == NULL)
246 {
9075806c 247 subnet = FindSubnetForNode(dwZoneId, dwIpAddr);
baa5324c
AK
248 if (subnet != NULL)
249 {
250 BOOL found = subnet->findMacAddress(dwIpAddr, nodeMacAddr);
251 if (!found)
252 {
22d657d2 253 DbgPrintf(6, _T("GetOldNodeWithNewIP: MAC address not found"));
baa5324c
AK
254 return NULL;
255 }
256 }
22d657d2
VK
257 else
258 {
259 DbgPrintf(6, _T("GetOldNodeWithNewIP: subnet not found"));
260 return NULL;
261 }
baa5324c
AK
262 }
263 else
264 {
265 memcpy(nodeMacAddr, bMacAddr, MAC_ADDR_LENGTH);
266 }
267
22d657d2 268 Interface *iface = FindInterfaceByMAC(nodeMacAddr);
baa5324c 269
22d657d2
VK
270 if (iface == NULL)
271 DbgPrintf(6, _T("GetOldNodeWithNewIP: returning null (FindInterfaceByMAC!)"));
baa5324c 272
22d657d2 273 return iface;
baa5324c 274}
5039dede 275
5db0d494
VK
276/**
277 * Check if host at given IP address is reachable by NetXMS server
278 */
967893bb 279static bool HostIsReachable(UINT32 ipAddr, UINT32 zoneId, bool fullCheck, SNMP_Transport **transport, AgentConnection **agentConn)
5db0d494
VK
280{
281 bool reachable = false;
282
283 if (transport != NULL)
284 *transport = NULL;
285 if (agentConn != NULL)
286 *agentConn = NULL;
287
967893bb
VK
288 UINT32 agentProxy = 0;
289 UINT32 icmpProxy = 0;
290 UINT32 snmpProxy = 0;
5db0d494
VK
291
292 if (IsZoningEnabled() && (zoneId != 0))
293 {
294 Zone *zone = (Zone *)g_idxZoneByGUID.get(zoneId);
295 if (zone != NULL)
296 {
297 agentProxy = zone->getAgentProxy();
298 icmpProxy = zone->getIcmpProxy();
299 snmpProxy = zone->getSnmpProxy();
300 }
301 }
302
303 // *** ICMP PING ***
304 if (icmpProxy != 0)
305 {
306 Node *proxyNode = (Node *)g_idxNodeById.get(icmpProxy);
307 if ((proxyNode != NULL) && proxyNode->isNativeAgent() && !proxyNode->isDown())
308 {
309 AgentConnection *conn = proxyNode->createAgentConnection();
310 if (conn != NULL)
311 {
312 TCHAR parameter[64], buffer[64];
313
314 _sntprintf(parameter, 64, _T("Icmp.Ping(%s)"), IpToStr(ipAddr, buffer));
315 if (conn->getParameter(parameter, 64, buffer) == ERR_SUCCESS)
316 {
317 TCHAR *eptr;
318 long value = _tcstol(buffer, &eptr, 10);
319 if ((*eptr == 0) && (value >= 0))
320 {
321 if (value < 10000)
322 {
323 reachable = true;
324 }
325 }
326 }
327 conn->disconnect();
328 delete conn;
329 }
330 }
331 }
332 else // not using ICMP proxy
333 {
c59466d2 334 if (IcmpPing(htonl(ipAddr), 3, g_icmpPingTimeout, NULL, g_icmpPingSize) == ICMP_SUCCESS)
5db0d494
VK
335 reachable = true;
336 }
337
338 if (reachable && !fullCheck)
339 return true;
340
341 // *** NetXMS agent ***
1938d609 342 AgentConnection *pAgentConn = new AgentConnectionEx(0, htonl(ipAddr), AGENT_LISTEN_PORT, AUTH_NONE, _T(""));
5db0d494
VK
343 if (agentProxy != 0)
344 {
345 Node *proxyNode = (Node *)g_idxNodeById.get(agentProxy);
346 if (proxyNode != NULL)
347 {
348 pAgentConn->setProxy(htonl(proxyNode->IpAddr()), proxyNode->getAgentPort(),
349 proxyNode->getAuthMethod(), proxyNode->getSharedSecret());
350 }
351 }
1938d609
VK
352 UINT32 rcc;
353 if (!pAgentConn->connect(g_pServerKey, FALSE, &rcc))
354 {
355 // If there are authentication problem, try default shared secret
356 if ((rcc == ERR_AUTH_REQUIRED) || (rcc == ERR_AUTH_FAILED))
357 {
358 TCHAR secret[MAX_SECRET_LENGTH];
359 ConfigReadStr(_T("AgentDefaultSharedSecret"), secret, MAX_SECRET_LENGTH, _T("netxms"));
360 pAgentConn->setAuthData(AUTH_SHA1_HASH, secret);
361 pAgentConn->connect(g_pServerKey, FALSE, &rcc);
362 }
363 }
364 if (rcc == ERR_SUCCESS)
5db0d494
VK
365 {
366 if (agentConn != NULL)
367 {
368 *agentConn = pAgentConn;
369 pAgentConn = NULL; // prevent deletion
370 }
371 reachable = true;
372 }
373 delete pAgentConn;
374
375 if (reachable && !fullCheck)
376 return true;
377
378 // *** SNMP ***
379 SNMP_Transport *pTransport;
380 if (snmpProxy != 0)
381 {
382 Node *proxyNode = (Node *)g_idxNodeById.get(snmpProxy);
383 if (proxyNode != NULL)
384 {
385 AgentConnection *pConn;
386
387 pConn = proxyNode->createAgentConnection();
388 if (pConn != NULL)
389 {
390 pTransport = new SNMP_ProxyTransport(pConn, ipAddr, 161);
391 }
392 }
393 }
394 else
395 {
396 pTransport = new SNMP_UDPTransport;
397 ((SNMP_UDPTransport *)pTransport)->createUDPTransport(NULL, htonl(ipAddr), 161);
398 }
399 int version;
29053453
VK
400 StringList oids;
401 oids.add(_T(".1.3.6.1.2.1.1.2.0"));
402 oids.add(_T(".1.3.6.1.2.1.1.1.0"));
403 AddDriverSpecificOids(&oids);
404 SNMP_SecurityContext *ctx = SnmpCheckCommSettings(pTransport, &version, NULL, &oids);
5db0d494
VK
405 if (ctx != NULL)
406 {
407 delete ctx;
408 if (transport != NULL)
409 {
410 pTransport->setSnmpVersion(version);
411 *transport = pTransport;
412 pTransport = NULL; // prevent deletion
413 }
414 reachable = true;
415 }
416 delete pTransport;
417
418 return reachable;
419}
420
22d657d2
VK
421/**
422 * Check if newly discovered node should be added
423 */
967893bb 424static BOOL AcceptNewNode(UINT32 dwIpAddr, UINT32 dwNetMask, UINT32 zoneId, BYTE *macAddr)
5039dede
AK
425{
426 DISCOVERY_FILTER_DATA data;
df8a4ca2 427 TCHAR szFilter[MAX_DB_STRING], szBuffer[256], szIpAddr[16];
967893bb 428 UINT32 dwTemp;
5039dede
AK
429 AgentConnection *pAgentConn;
430 NXSL_Program *pScript;
431 NXSL_Value *pValue;
432 BOOL bResult = FALSE;
5db0d494 433 SNMP_Transport *pTransport;
5039dede
AK
434
435 IpToStr(dwIpAddr, szIpAddr);
89135050
VK
436 if ((FindNodeByIP(zoneId, dwIpAddr) != NULL) ||
437 (FindSubnetByIP(zoneId, dwIpAddr) != NULL))
5039dede 438 {
35f836fe 439 DbgPrintf(4, _T("AcceptNewNode(%s): node already exist in database"), szIpAddr);
5039dede
AK
440 return FALSE; // Node already exist in database
441 }
442
22d657d2
VK
443 Interface *iface = GetOldNodeWithNewIP(dwIpAddr, dwNetMask, zoneId, macAddr);
444 if (iface != NULL)
baa5324c 445 {
5db0d494
VK
446 if (!HostIsReachable(dwIpAddr, zoneId, false, NULL, NULL))
447 {
448 DbgPrintf(4, _T("AcceptNewNode(%s): found existing interface with same MAC address, but new IP is not reachable"), szIpAddr);
449 return FALSE;
450 }
451
22d657d2
VK
452 Node *oldNode = iface->getParentNode();
453 if (iface->IpAddr() == oldNode->IpAddr())
454 {
455 // we should change node's primary IP only if old IP for this MAC was also node's primary IP
456 TCHAR szOldIpAddr[16];
457 IpToStr(oldNode->IpAddr(), szOldIpAddr);
458 DbgPrintf(4, _T("AcceptNewNode(%s): node already exist in database with ip %s, will change to new"),
459 szIpAddr, szOldIpAddr);
460 oldNode->changeIPAddress(dwIpAddr);
461 }
baa5324c
AK
462 return FALSE;
463 }
464
496390c2 465 // Allow filtering by loaded modules
967893bb 466 for(UINT32 i = 0; i < g_dwNumModules; i++)
496390c2
VK
467 {
468 if (g_pModuleList[i].pfAcceptNewNode != NULL)
469 {
470 if (!g_pModuleList[i].pfAcceptNewNode(dwIpAddr, dwNetMask, zoneId, macAddr))
471 return FALSE; // filtered out by module
472 }
473 }
474
5039dede 475 // Read configuration
35f836fe 476 ConfigReadStr(_T("DiscoveryFilter"), szFilter, MAX_DB_STRING, _T(""));
5039dede
AK
477 StrStrip(szFilter);
478
56e92fc9 479 // Check for filter script
35f836fe 480 if ((szFilter[0] == 0) || (!_tcsicmp(szFilter, _T("none"))))
5039dede 481 {
5db0d494
VK
482 if (!HostIsReachable(dwIpAddr, zoneId, false, NULL, NULL))
483 {
484 DbgPrintf(4, _T("AcceptNewNode(%s): host is not reachable"), szIpAddr);
485 return FALSE;
486 }
35f836fe 487 DbgPrintf(4, _T("AcceptNewNode(%s): no filtering, node accepted"), szIpAddr);
5039dede
AK
488 return TRUE; // No filtering
489 }
490
491 // Initialize new node data
492 memset(&data, 0, sizeof(DISCOVERY_FILTER_DATA));
493 data.dwIpAddr = dwIpAddr;
494 data.dwNetMask = dwNetMask;
495 data.dwSubnetAddr = dwIpAddr & dwNetMask;
496
56e92fc9 497 // Check for address range if we use simple filter instead of script
967893bb 498 UINT32 autoFilterFlags;
56e92fc9
VK
499 if (!_tcsicmp(szFilter, _T("auto")))
500 {
501 autoFilterFlags = ConfigReadULong(_T("DiscoveryFilterFlags"), DFF_ALLOW_AGENT | DFF_ALLOW_SNMP);
502 DbgPrintf(4, _T("AcceptNewNode(%s): auto filter, flags=%04X"), szIpAddr, autoFilterFlags);
503
504 if (autoFilterFlags & DFF_ONLY_RANGE)
505 {
506 DbgPrintf(4, _T("AcceptNewNode(%s): auto filter - checking range"), szIpAddr);
507 DB_RESULT hResult = DBSelect(g_hCoreDB, _T("SELECT addr_type,addr1,addr2 FROM address_lists WHERE list_type=2"));
508 if (hResult != NULL)
509 {
510 int nRows = DBGetNumRows(hResult);
511 for(int i = 0; (i < nRows) && (!bResult); i++)
512 {
513 int nType = DBGetFieldLong(hResult, i, 0);
514 if (nType == 0)
515 {
516 // Subnet
517 bResult = (data.dwIpAddr & DBGetFieldIPAddr(hResult, i, 2)) == DBGetFieldIPAddr(hResult, i, 1);
518 }
519 else
520 {
521 // Range
522 bResult = ((data.dwIpAddr >= DBGetFieldIPAddr(hResult, i, 1)) &&
523 (data.dwIpAddr <= DBGetFieldIPAddr(hResult, i, 2)));
524 }
525 }
526 DBFreeResult(hResult);
527 }
3b71dbbb
VK
528 DbgPrintf(4, _T("AcceptNewNode(%s): auto filter - range check result is %d"), szIpAddr, bResult);
529 if (!bResult)
530 return FALSE;
56e92fc9 531 }
56e92fc9
VK
532 }
533
5db0d494
VK
534 // Check if host is reachable
535 if (!HostIsReachable(dwIpAddr, zoneId, true, &pTransport, &pAgentConn))
536 {
537 DbgPrintf(4, _T("AcceptNewNode(%s): host is not reachable"), szIpAddr);
538 return FALSE;
539 }
540 if (pTransport != NULL)
5039dede
AK
541 {
542 data.dwFlags |= NNF_IS_SNMP;
5db0d494 543 data.nSNMPVersion = pTransport->getSnmpVersion();
5039dede 544 }
5db0d494 545 if (pAgentConn != NULL)
5039dede
AK
546 {
547 data.dwFlags |= NNF_IS_AGENT;
4687826e
VK
548 pAgentConn->getParameter(_T("Agent.Version"), MAX_AGENT_VERSION_LEN, data.szAgentVersion);
549 pAgentConn->getParameter(_T("System.PlatformName"), MAX_PLATFORM_NAME_LEN, data.szPlatform);
5039dede
AK
550 }
551
552 // Check if node is a router
553 if (data.dwFlags & NNF_IS_SNMP)
554 {
5d2c5741 555 if (SnmpGet(data.nSNMPVersion, pTransport,
967893bb 556 _T(".1.3.6.1.2.1.4.1.0"), NULL, 0, &dwTemp, sizeof(UINT32), 0) == SNMP_ERR_SUCCESS)
5039dede
AK
557 {
558 if (dwTemp == 1)
559 data.dwFlags |= NNF_IS_ROUTER;
560 }
561 }
562 else if (data.dwFlags & NNF_IS_AGENT)
563 {
564 // Check IP forwarding status
4687826e 565 if (pAgentConn->getParameter(_T("Net.IP.Forwarding"), 16, szBuffer) == ERR_SUCCESS)
5039dede
AK
566 {
567 if (_tcstoul(szBuffer, NULL, 10) != 0)
568 data.dwFlags |= NNF_IS_ROUTER;
569 }
570 }
571
572 // Check various SNMP device capabilities
573 if (data.dwFlags & NNF_IS_SNMP)
574 {
575 // Get SNMP OID
5d2c5741 576 SnmpGet(data.nSNMPVersion, pTransport,
f42b8099 577 _T(".1.3.6.1.2.1.1.2.0"), NULL, 0, data.szObjectId, MAX_OID_LEN * 4, 0);
5039dede
AK
578
579 // Check if node is a bridge
5d2c5741 580 if (SnmpGet(data.nSNMPVersion, pTransport,
d525c9ed 581 _T(".1.3.6.1.2.1.17.1.1.0"), NULL, 0, szBuffer, sizeof(szBuffer), 0) == SNMP_ERR_SUCCESS)
5039dede
AK
582 {
583 data.dwFlags |= NNF_IS_BRIDGE;
584 }
585
586 // Check for CDP (Cisco Discovery Protocol) support
5d2c5741 587 if (SnmpGet(data.nSNMPVersion, pTransport,
967893bb 588 _T(".1.3.6.1.4.1.9.9.23.1.3.1.0"), NULL, 0, &dwTemp, sizeof(UINT32), 0) == SNMP_ERR_SUCCESS)
5039dede
AK
589 {
590 if (dwTemp == 1)
591 data.dwFlags |= NNF_IS_CDP;
592 }
593
594 // Check for SONMP (Nortel topology discovery protocol) support
5d2c5741 595 if (SnmpGet(data.nSNMPVersion, pTransport,
967893bb 596 _T(".1.3.6.1.4.1.45.1.6.13.1.2.0"), NULL, 0, &dwTemp, sizeof(UINT32), 0) == SNMP_ERR_SUCCESS)
5039dede
AK
597 {
598 if (dwTemp == 1)
599 data.dwFlags |= NNF_IS_SONMP;
600 }
601
602 // Check for LLDP (Link Layer Discovery Protocol) support
5d2c5741 603 if (SnmpGet(data.nSNMPVersion, pTransport,
d525c9ed 604 _T(".1.0.8802.1.1.2.1.3.2.0"), NULL, 0, szBuffer, sizeof(szBuffer), 0) == SNMP_ERR_SUCCESS)
5039dede
AK
605 {
606 data.dwFlags |= NNF_IS_LLDP;
607 }
608 }
609
610 // Cleanup
611 delete pAgentConn;
612 delete pTransport;
613
614 // Check if we use simple filter instead of script
35f836fe 615 if (!_tcsicmp(szFilter, _T("auto")))
5039dede 616 {
56e92fc9
VK
617 bResult = FALSE;
618 if ((autoFilterFlags & (DFF_ALLOW_AGENT | DFF_ALLOW_SNMP)) == 0)
5039dede
AK
619 {
620 bResult = TRUE;
621 }
622 else
623 {
56e92fc9 624 if (autoFilterFlags & DFF_ALLOW_AGENT)
5039dede
AK
625 {
626 if (data.dwFlags & NNF_IS_AGENT)
627 bResult = TRUE;
628 }
629
56e92fc9 630 if (autoFilterFlags & DFF_ALLOW_SNMP)
5039dede
AK
631 {
632 if (data.dwFlags & NNF_IS_SNMP)
633 bResult = TRUE;
634 }
635 }
35f836fe 636 DbgPrintf(4, _T("AcceptNewNode(%s): auto filter - bResult=%d"), szIpAddr, bResult);
5039dede
AK
637 }
638 else
639 {
bb5365ed
VK
640 g_pScriptLibrary->lock();
641 pScript = g_pScriptLibrary->findScript(szFilter);
5039dede
AK
642 if (pScript != NULL)
643 {
35f836fe 644 DbgPrintf(4, _T("AcceptNewNode(%s): Running filter script %s"), szIpAddr, szFilter);
5039dede 645 pValue = new NXSL_Value(new NXSL_Object(&m_nxslDiscoveryClass, &data));
13ceb0fb 646 if (pScript->run(new NXSL_ServerEnv, 1, &pValue) == 0)
5039dede 647 {
bb5365ed 648 bResult = (pScript->getResult()->getValueAsInt32() != 0) ? TRUE : FALSE;
35f836fe 649 DbgPrintf(4, _T("AcceptNewNode(%s): Filter script result: %d"), szIpAddr, bResult);
5039dede
AK
650 }
651 else
652 {
35f836fe 653 DbgPrintf(4, _T("AcceptNewNode(%s): Filter script execution error: %s"),
bb5365ed 654 szIpAddr, pScript->getErrorText());
35f836fe 655 PostEvent(EVENT_SCRIPT_ERROR, g_dwMgmtNode, "ssd", szFilter,
bb5365ed 656 pScript->getErrorText(), 0);
5039dede
AK
657 }
658 }
659 else
660 {
35f836fe 661 DbgPrintf(4, _T("AcceptNewNode(%s): Cannot find filter script %s"), szIpAddr, szFilter);
5039dede 662 }
bb5365ed 663 g_pScriptLibrary->unlock();
5039dede
AK
664 }
665
666 return bResult;
667}
668
496390c2
VK
669/**
670 * Node poller thread (poll new nodes and put them into the database)
671 */
5039dede
AK
672THREAD_RESULT THREAD_CALL NodePoller(void *arg)
673{
674 NEW_NODE *pInfo;
675 TCHAR szIpAddr[16], szNetMask[16];
676
35f836fe 677 DbgPrintf(1, _T("Node poller started"));
5039dede 678
89135050 679 while(!IsShutdownInProgress())
5039dede
AK
680 {
681 pInfo = (NEW_NODE *)g_nodePollerQueue.GetOrBlock();
682 if (pInfo == INVALID_POINTER_VALUE)
683 break; // Shutdown indicator received
684
dc25b21c
VK
685 DbgPrintf(4, _T("NodePoller: processing node %s/%s in zone %d"),
686 IpToStr(pInfo->dwIpAddr, szIpAddr), IpToStr(pInfo->dwNetMask, szNetMask), (int)pInfo->zoneId);
e02953a4 687 if (pInfo->ignoreFilter || AcceptNewNode(pInfo->dwIpAddr, pInfo->dwNetMask, pInfo->zoneId, pInfo->bMacAddr))
07f6978d 688 {
ff6d2c5d
VK
689 ObjectTransactionStart();
690 PollNewNode(pInfo->dwIpAddr, pInfo->dwNetMask, 0, 0, 0, NULL, 0, 0, NULL, pInfo->zoneId, true, true);
691 ObjectTransactionEnd();
07f6978d 692 }
5039dede
AK
693 free(pInfo);
694 }
35f836fe 695 DbgPrintf(1, _T("Node poller thread terminated"));
5039dede
AK
696 return THREAD_OK;
697}