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