2fac7a95cafe6571338af2c6251c0d7f59c5543b
[public/netxms.git] / src / server / core / np.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2017 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 setName(_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 zoneUIN 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 zoneUIN, 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)zoneUIN);
154 // Check for node existence
155 if ((FindNodeByIP(zoneUIN, ipAddr) != NULL) ||
156 (FindSubnetByIP(zoneUIN, 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, 0, agentProxyId, snmpProxyId, icmpProxyId, sshProxyId, zoneUIN);
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 TCHAR *tmp;
183 if(IsZoningEnabled() && zoneUIN != 0)
184 {
185 AgentConnectionEx *conn = pNode->getConnectionToZoneNodeProxy();
186 tmp = conn != NULL ? conn->getHostByAddr(ipAddr, dnsName, MAX_DNS_NAME) : NULL;
187 }
188 else
189 {
190 tmp = ipAddr.getHostByAddr(dnsName, MAX_DNS_NAME);
191 }
192
193 if(tmp != NULL)
194 {
195 if(ResolveHostName(zoneUIN, dnsName).equals(ipAddr))
196 {
197 // We have valid DNS name which resolves back to node's IP address, use it as primary name
198 pNode->setPrimaryName(dnsName);
199 DbgPrintf(4, _T("PollNode: Using DNS name %s as primary name for node %s"), dnsName, szIpAddr);
200 }
201 }
202 }
203
204 // Bind node to cluster before first configuration poll
205 if (cluster != NULL)
206 {
207 cluster->applyToTarget(pNode);
208 }
209
210 if (creationFlags & NXC_NCF_CREATE_UNMANAGED)
211 {
212 pNode->setMgmtStatus(FALSE);
213 pNode->checkSubnetBinding();
214 }
215
216 if (doConfPoll)
217 {
218 pNode->configurationPollWorkerEntry(RegisterPoller(POLLER_TYPE_CONFIGURATION, pNode));
219 }
220
221 pNode->unhide();
222 PostEvent(EVENT_NODE_ADDED, pNode->getId(), "d", (int)(discoveredNode ? 1 : 0));
223
224 return pNode;
225 }
226
227 /**
228 * Find existing node by MAC address to detect IP address change for already known node.
229 *
230 * @param ipAddr new (discovered) IP address
231 * @param zoneUIN zone ID
232 * @param bMacAddr MAC address of discovered node, or NULL if not known
233 *
234 * @return pointer to existing interface object with given MAC address or NULL if no such interface found
235 */
236 static Interface *GetOldNodeWithNewIP(const InetAddress& ipAddr, UINT32 zoneUIN, BYTE *bMacAddr)
237 {
238 Subnet *subnet;
239 BYTE nodeMacAddr[MAC_ADDR_LENGTH];
240 TCHAR szIpAddr[64], szMacAddr[20];
241
242 ipAddr.toString(szIpAddr);
243 MACToStr(bMacAddr, szMacAddr);
244 DbgPrintf(6, _T("GetOldNodeWithNewIP: ip=%s mac=%s"), szIpAddr, szMacAddr);
245
246 if (bMacAddr == NULL)
247 {
248 subnet = FindSubnetForNode(zoneUIN, ipAddr);
249 if (subnet != NULL)
250 {
251 BOOL found = subnet->findMacAddress(ipAddr, nodeMacAddr);
252 if (!found)
253 {
254 DbgPrintf(6, _T("GetOldNodeWithNewIP: MAC address not found"));
255 return NULL;
256 }
257 }
258 else
259 {
260 DbgPrintf(6, _T("GetOldNodeWithNewIP: subnet not found"));
261 return NULL;
262 }
263 }
264 else
265 {
266 memcpy(nodeMacAddr, bMacAddr, MAC_ADDR_LENGTH);
267 }
268
269 Interface *iface = FindInterfaceByMAC(nodeMacAddr);
270
271 if (iface == NULL)
272 DbgPrintf(6, _T("GetOldNodeWithNewIP: returning null (FindInterfaceByMAC!)"));
273
274 return iface;
275 }
276
277 /**
278 * Check if host at given IP address is reachable by NetXMS server
279 */
280 static bool HostIsReachable(const InetAddress& ipAddr, UINT32 zoneUIN, bool fullCheck, SNMP_Transport **transport, AgentConnection **agentConn)
281 {
282 bool reachable = false;
283
284 if (transport != NULL)
285 *transport = NULL;
286 if (agentConn != NULL)
287 *agentConn = NULL;
288
289 UINT32 agentProxy = 0;
290 UINT32 icmpProxy = 0;
291 UINT32 snmpProxy = 0;
292
293 if (IsZoningEnabled() && (zoneUIN != 0))
294 {
295 Zone *zone = FindZoneByUIN(zoneUIN);
296 if (zone != NULL)
297 {
298 agentProxy = zone->getProxyNodeId();
299 icmpProxy = zone->getProxyNodeId();
300 snmpProxy = zone->getProxyNodeId();
301 }
302 }
303
304 // *** ICMP PING ***
305 if (icmpProxy != 0)
306 {
307 Node *proxyNode = (Node *)g_idxNodeById.get(icmpProxy);
308 if ((proxyNode != NULL) && proxyNode->isNativeAgent() && !proxyNode->isDown())
309 {
310 AgentConnection *conn = proxyNode->createAgentConnection();
311 if (conn != NULL)
312 {
313 TCHAR parameter[128], buffer[64];
314
315 _sntprintf(parameter, 128, _T("Icmp.Ping(%s)"), ipAddr.toString(buffer));
316 if (conn->getParameter(parameter, 64, buffer) == ERR_SUCCESS)
317 {
318 TCHAR *eptr;
319 long value = _tcstol(buffer, &eptr, 10);
320 if ((*eptr == 0) && (value >= 0))
321 {
322 if (value < 10000)
323 {
324 reachable = true;
325 }
326 }
327 }
328 conn->decRefCount();
329 }
330 }
331 }
332 else // not using ICMP proxy
333 {
334 if (IcmpPing(ipAddr, 3, g_icmpPingTimeout, NULL, g_icmpPingSize) == ICMP_SUCCESS)
335 reachable = true;
336 }
337
338 if (reachable && !fullCheck)
339 return true;
340
341 // *** NetXMS agent ***
342 AgentConnection *pAgentConn = new AgentConnectionEx(0, ipAddr, AGENT_LISTEN_PORT, AUTH_NONE, _T(""));
343 if (agentProxy != 0)
344 {
345 Node *proxyNode = (Node *)g_idxNodeById.get(agentProxy);
346 if (proxyNode != NULL)
347 {
348 pAgentConn->setProxy(proxyNode->getIpAddress(), proxyNode->getAgentPort(),
349 proxyNode->getAgentAuthMethod(), proxyNode->getSharedSecret());
350 }
351 }
352 pAgentConn->setCommandTimeout(g_agentCommandTimeout);
353 UINT32 rcc;
354 if (!pAgentConn->connect(g_pServerKey, &rcc))
355 {
356 // If there are authentication problem, try default shared secret
357 if ((rcc == ERR_AUTH_REQUIRED) || (rcc == ERR_AUTH_FAILED))
358 {
359 TCHAR secret[MAX_SECRET_LENGTH];
360 ConfigReadStr(_T("AgentDefaultSharedSecret"), secret, MAX_SECRET_LENGTH, _T("netxms"));
361 DecryptPassword(_T("netxms"), secret, secret, MAX_SECRET_LENGTH);
362
363 pAgentConn->setAuthData(AUTH_SHA1_HASH, secret);
364 pAgentConn->connect(g_pServerKey, &rcc);
365 }
366 }
367 if (rcc == ERR_SUCCESS)
368 {
369 if (agentConn != NULL)
370 {
371 *agentConn = pAgentConn;
372 pAgentConn = NULL; // prevent deletion
373 }
374 reachable = true;
375 }
376 if (pAgentConn != NULL)
377 pAgentConn->decRefCount();
378
379 if (reachable && !fullCheck)
380 return true;
381
382 // *** SNMP ***
383 INT16 version;
384 StringList oids;
385 oids.add(_T(".1.3.6.1.2.1.1.2.0"));
386 oids.add(_T(".1.3.6.1.2.1.1.1.0"));
387 AddDriverSpecificOids(&oids);
388 SNMP_Transport *pTransport = SnmpCheckCommSettings(snmpProxy, ipAddr, &version, 0, NULL, &oids);
389 //pass correct port
390 if (pTransport != NULL)
391 {
392 if (transport != NULL)
393 {
394 pTransport->setSnmpVersion(version);
395 *transport = pTransport;
396 pTransport = NULL; // prevent deletion
397 }
398 reachable = true;
399 delete pTransport;
400 }
401
402 return reachable;
403 }
404
405 /**
406 * Check if newly discovered node should be added
407 */
408 static BOOL AcceptNewNode(const InetAddress& addr, UINT32 zoneUIN, BYTE *macAddr)
409 {
410 DISCOVERY_FILTER_DATA data;
411 TCHAR szFilter[MAX_CONFIG_VALUE], szBuffer[256], szIpAddr[64];
412 UINT32 dwTemp;
413 AgentConnection *pAgentConn;
414 BOOL bResult = FALSE;
415 SNMP_Transport *pTransport;
416
417 addr.toString(szIpAddr);
418 if ((FindNodeByIP(zoneUIN, addr) != NULL) ||
419 (FindSubnetByIP(zoneUIN, addr) != NULL))
420 {
421 DbgPrintf(4, _T("AcceptNewNode(%s): node already exist in database"), szIpAddr);
422 return FALSE; // Node already exist in database
423 }
424
425 if (!memcmp(macAddr, "\xFF\xFF\xFF\xFF\xFF\xFF", 6))
426 {
427 DbgPrintf(4, _T("AcceptNewNode(%s): broadcast MAC address"), szIpAddr);
428 return FALSE; // Broadcast MAC
429 }
430
431 NXSL_VM *hook = FindHookScript(_T("AcceptNewNode"));
432 if (hook != NULL)
433 {
434 bool stop = false;
435 hook->setGlobalVariable(_T("$ipAddr"), new NXSL_Value(szIpAddr));
436 hook->setGlobalVariable(_T("$ipNetMask"), new NXSL_Value(addr.getMaskBits()));
437 MACToStr(macAddr, szBuffer);
438 hook->setGlobalVariable(_T("$macAddr"), new NXSL_Value(szBuffer));
439 hook->setGlobalVariable(_T("$zoneUIN"), new NXSL_Value(zoneUIN));
440 if (hook->run())
441 {
442 NXSL_Value *result = hook->getResult();
443 if (result->isZero())
444 {
445 stop = true;
446 DbgPrintf(4, _T("AcceptNewNode(%s): rejected by hook script"), szIpAddr);
447 }
448 }
449 else
450 {
451 DbgPrintf(4, _T("AcceptNewNode(%s): hook script execution error: %s"), szIpAddr, hook->getErrorText());
452 }
453 delete hook;
454 if (stop)
455 return FALSE; // blocked by hook
456 }
457
458 Interface *iface = GetOldNodeWithNewIP(addr, zoneUIN, macAddr);
459 if (iface != NULL)
460 {
461 if (!HostIsReachable(addr, zoneUIN, false, NULL, NULL))
462 {
463 DbgPrintf(4, _T("AcceptNewNode(%s): found existing interface with same MAC address, but new IP is not reachable"), szIpAddr);
464 return FALSE;
465 }
466
467 Node *oldNode = iface->getParentNode();
468 if (iface->getIpAddressList()->hasAddress(oldNode->getIpAddress()))
469 {
470 // we should change node's primary IP only if old IP for this MAC was also node's primary IP
471 TCHAR szOldIpAddr[16];
472 oldNode->getIpAddress().toString(szOldIpAddr);
473 DbgPrintf(4, _T("AcceptNewNode(%s): node already exist in database with ip %s, will change to new"), szIpAddr, szOldIpAddr);
474 oldNode->changeIPAddress(addr);
475 }
476 return FALSE;
477 }
478
479 // Allow filtering by loaded modules
480 for(UINT32 i = 0; i < g_dwNumModules; i++)
481 {
482 if (g_pModuleList[i].pfAcceptNewNode != NULL)
483 {
484 if (!g_pModuleList[i].pfAcceptNewNode(addr, zoneUIN, macAddr))
485 return FALSE; // filtered out by module
486 }
487 }
488
489 // Read configuration
490 ConfigReadStr(_T("DiscoveryFilter"), szFilter, MAX_CONFIG_VALUE, _T(""));
491 StrStrip(szFilter);
492
493 // Check for filter script
494 if ((szFilter[0] == 0) || (!_tcsicmp(szFilter, _T("none"))))
495 {
496 if (!HostIsReachable(addr, zoneUIN, false, NULL, NULL))
497 {
498 DbgPrintf(4, _T("AcceptNewNode(%s): host is not reachable"), szIpAddr);
499 return FALSE;
500 }
501 DbgPrintf(4, _T("AcceptNewNode(%s): no filtering, node accepted"), szIpAddr);
502 return TRUE; // No filtering
503 }
504
505 // Initialize new node data
506 memset(&data, 0, sizeof(DISCOVERY_FILTER_DATA));
507 data.ipAddr = addr;
508
509 // Check for address range if we use simple filter instead of script
510 UINT32 autoFilterFlags;
511 if (!_tcsicmp(szFilter, _T("auto")))
512 {
513 autoFilterFlags = ConfigReadULong(_T("DiscoveryFilterFlags"), DFF_ALLOW_AGENT | DFF_ALLOW_SNMP);
514 DbgPrintf(4, _T("AcceptNewNode(%s): auto filter, flags=%04X"), szIpAddr, autoFilterFlags);
515
516 if (autoFilterFlags & DFF_ONLY_RANGE)
517 {
518 DbgPrintf(4, _T("AcceptNewNode(%s): auto filter - checking range"), szIpAddr);
519 DB_HANDLE hdb = DBConnectionPoolAcquireConnection();
520 DB_RESULT hResult = DBSelect(hdb, _T("SELECT addr_type,addr1,addr2 FROM address_lists WHERE list_type=2"));
521 if (hResult != NULL)
522 {
523 int nRows = DBGetNumRows(hResult);
524 for(int i = 0; (i < nRows) && (!bResult); i++)
525 {
526 bResult = InetAddressListElement(hResult, i).contains(data.ipAddr);
527 }
528 DBFreeResult(hResult);
529 }
530 DBConnectionPoolReleaseConnection(hdb);
531 DbgPrintf(4, _T("AcceptNewNode(%s): auto filter - range check result is %d"), szIpAddr, bResult);
532 if (!bResult)
533 return FALSE;
534 }
535 }
536
537 // Check if host is reachable
538 if (!HostIsReachable(addr, zoneUIN, true, &pTransport, &pAgentConn))
539 {
540 DbgPrintf(4, _T("AcceptNewNode(%s): host is not reachable"), szIpAddr);
541 return FALSE;
542 }
543 if (pTransport != NULL)
544 {
545 data.dwFlags |= NNF_IS_SNMP;
546 data.nSNMPVersion = pTransport->getSnmpVersion();
547 }
548 if (pAgentConn != NULL)
549 {
550 data.dwFlags |= NNF_IS_AGENT;
551 pAgentConn->getParameter(_T("Agent.Version"), MAX_AGENT_VERSION_LEN, data.szAgentVersion);
552 pAgentConn->getParameter(_T("System.PlatformName"), MAX_PLATFORM_NAME_LEN, data.szPlatform);
553 }
554
555 // Check if node is a router
556 if (data.dwFlags & NNF_IS_SNMP)
557 {
558 if (SnmpGet(data.nSNMPVersion, pTransport,
559 _T(".1.3.6.1.2.1.4.1.0"), NULL, 0, &dwTemp, sizeof(UINT32), 0) == SNMP_ERR_SUCCESS)
560 {
561 if (dwTemp == 1)
562 data.dwFlags |= NNF_IS_ROUTER;
563 }
564 }
565 else if (data.dwFlags & NNF_IS_AGENT)
566 {
567 // Check IP forwarding status
568 if (pAgentConn->getParameter(_T("Net.IP.Forwarding"), 16, szBuffer) == ERR_SUCCESS)
569 {
570 if (_tcstoul(szBuffer, NULL, 10) != 0)
571 data.dwFlags |= NNF_IS_ROUTER;
572 }
573 }
574
575 // Check various SNMP device capabilities
576 if (data.dwFlags & NNF_IS_SNMP)
577 {
578 // Get SNMP OID
579 SnmpGet(data.nSNMPVersion, pTransport,
580 _T(".1.3.6.1.2.1.1.2.0"), NULL, 0, data.szObjectId, MAX_OID_LEN * 4, 0);
581
582 // Check if node is a bridge
583 if (SnmpGet(data.nSNMPVersion, pTransport,
584 _T(".1.3.6.1.2.1.17.1.1.0"), NULL, 0, szBuffer, sizeof(szBuffer), 0) == SNMP_ERR_SUCCESS)
585 {
586 data.dwFlags |= NNF_IS_BRIDGE;
587 }
588
589 // Check for CDP (Cisco Discovery Protocol) support
590 if (SnmpGet(data.nSNMPVersion, pTransport,
591 _T(".1.3.6.1.4.1.9.9.23.1.3.1.0"), NULL, 0, &dwTemp, sizeof(UINT32), 0) == SNMP_ERR_SUCCESS)
592 {
593 if (dwTemp == 1)
594 data.dwFlags |= NNF_IS_CDP;
595 }
596
597 // Check for SONMP (Nortel topology discovery protocol) support
598 if (SnmpGet(data.nSNMPVersion, pTransport,
599 _T(".1.3.6.1.4.1.45.1.6.13.1.2.0"), NULL, 0, &dwTemp, sizeof(UINT32), 0) == SNMP_ERR_SUCCESS)
600 {
601 if (dwTemp == 1)
602 data.dwFlags |= NNF_IS_SONMP;
603 }
604
605 // Check for LLDP (Link Layer Discovery Protocol) support
606 if (SnmpGet(data.nSNMPVersion, pTransport,
607 _T(".1.0.8802.1.1.2.1.3.2.0"), NULL, 0, szBuffer, sizeof(szBuffer), 0) == SNMP_ERR_SUCCESS)
608 {
609 data.dwFlags |= NNF_IS_LLDP;
610 }
611 }
612
613 // Cleanup
614 if (pAgentConn != NULL)
615 pAgentConn->decRefCount();
616 delete pTransport;
617
618 // Check if we use simple filter instead of script
619 if (!_tcsicmp(szFilter, _T("auto")))
620 {
621 bResult = FALSE;
622 if ((autoFilterFlags & (DFF_ALLOW_AGENT | DFF_ALLOW_SNMP)) == 0)
623 {
624 bResult = TRUE;
625 }
626 else
627 {
628 if (autoFilterFlags & DFF_ALLOW_AGENT)
629 {
630 if (data.dwFlags & NNF_IS_AGENT)
631 bResult = TRUE;
632 }
633
634 if (autoFilterFlags & DFF_ALLOW_SNMP)
635 {
636 if (data.dwFlags & NNF_IS_SNMP)
637 bResult = TRUE;
638 }
639 }
640 DbgPrintf(4, _T("AcceptNewNode(%s): auto filter - bResult=%d"), szIpAddr, bResult);
641 }
642 else
643 {
644 NXSL_VM *vm = CreateServerScriptVM(szFilter);
645 if (vm != NULL)
646 {
647 DbgPrintf(4, _T("AcceptNewNode(%s): Running filter script %s"), szIpAddr, szFilter);
648 NXSL_Value *pValue = new NXSL_Value(new NXSL_Object(&m_nxslDiscoveryClass, &data));
649 if (vm->run(1, &pValue))
650 {
651 bResult = (vm->getResult()->getValueAsInt32() != 0) ? TRUE : FALSE;
652 DbgPrintf(4, _T("AcceptNewNode(%s): Filter script result: %d"), szIpAddr, bResult);
653 }
654 else
655 {
656 DbgPrintf(4, _T("AcceptNewNode(%s): Filter script execution error: %s"),
657 szIpAddr, vm->getErrorText());
658 PostEvent(EVENT_SCRIPT_ERROR, g_dwMgmtNode, "ssd", szFilter, vm->getErrorText(), 0);
659 }
660 delete vm;
661 }
662 else
663 {
664 DbgPrintf(4, _T("AcceptNewNode(%s): Cannot find filter script %s"), szIpAddr, szFilter);
665 }
666 }
667
668 return bResult;
669 }
670
671 /**
672 * Node poller thread (poll new nodes and put them into the database)
673 */
674 THREAD_RESULT THREAD_CALL NodePoller(void *arg)
675 {
676 NEW_NODE *pInfo;
677 TCHAR szIpAddr[64];
678
679 ThreadSetName("NodePoller");
680 nxlog_debug(1, _T("Node poller started"));
681
682 while(!IsShutdownInProgress())
683 {
684 pInfo = (NEW_NODE *)g_nodePollerQueue.getOrBlock();
685 if (pInfo == INVALID_POINTER_VALUE)
686 break; // Shutdown indicator received
687
688 DbgPrintf(4, _T("NodePoller: processing node %s/%d in zone %d"),
689 pInfo->ipAddr.toString(szIpAddr), pInfo->ipAddr.getMaskBits(), (int)pInfo->zoneUIN);
690 if (pInfo->ignoreFilter || AcceptNewNode(pInfo->ipAddr, pInfo->zoneUIN, pInfo->bMacAddr))
691 {
692 ObjectTransactionStart();
693 PollNewNode(pInfo->ipAddr, 0, 0, 0, NULL, 0, 0, 0, 0, NULL, NULL, NULL, pInfo->zoneUIN, true, true);
694 ObjectTransactionEnd();
695 }
696 free(pInfo);
697 }
698 nxlog_debug(1, _T("Node poller thread terminated"));
699 return THREAD_OK;
700 }