Default shared secret for agents can be set
[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 ***
342 AgentConnection *pAgentConn = new AgentConnection(htonl(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(htonl(proxyNode->IpAddr()), proxyNode->getAgentPort(),
349 proxyNode->getAuthMethod(), proxyNode->getSharedSecret());
350 }
351 }
352 if (pAgentConn->connect(g_pServerKey))
353 {
354 if (agentConn != NULL)
355 {
356 *agentConn = pAgentConn;
357 pAgentConn = NULL; // prevent deletion
358 }
359 reachable = true;
360 }
361 delete pAgentConn;
362
363 if (reachable && !fullCheck)
364 return true;
365
366 // *** SNMP ***
367 SNMP_Transport *pTransport;
368 if (snmpProxy != 0)
369 {
370 Node *proxyNode = (Node *)g_idxNodeById.get(snmpProxy);
371 if (proxyNode != NULL)
372 {
373 AgentConnection *pConn;
374
375 pConn = proxyNode->createAgentConnection();
376 if (pConn != NULL)
377 {
378 pTransport = new SNMP_ProxyTransport(pConn, ipAddr, 161);
379 }
380 }
381 }
382 else
383 {
384 pTransport = new SNMP_UDPTransport;
385 ((SNMP_UDPTransport *)pTransport)->createUDPTransport(NULL, htonl(ipAddr), 161);
386 }
387 int version;
cb1ae425 388 SNMP_SecurityContext *ctx = SnmpCheckCommSettings(pTransport, &version, NULL, NULL);
5db0d494
VK
389 if (ctx != NULL)
390 {
391 delete ctx;
392 if (transport != NULL)
393 {
394 pTransport->setSnmpVersion(version);
395 *transport = pTransport;
396 pTransport = NULL; // prevent deletion
397 }
398 reachable = true;
399 }
400 delete pTransport;
401
402 return reachable;
403}
404
22d657d2
VK
405/**
406 * Check if newly discovered node should be added
407 */
967893bb 408static BOOL AcceptNewNode(UINT32 dwIpAddr, UINT32 dwNetMask, UINT32 zoneId, BYTE *macAddr)
5039dede
AK
409{
410 DISCOVERY_FILTER_DATA data;
df8a4ca2 411 TCHAR szFilter[MAX_DB_STRING], szBuffer[256], szIpAddr[16];
967893bb 412 UINT32 dwTemp;
5039dede
AK
413 AgentConnection *pAgentConn;
414 NXSL_Program *pScript;
415 NXSL_Value *pValue;
416 BOOL bResult = FALSE;
5db0d494 417 SNMP_Transport *pTransport;
5039dede
AK
418
419 IpToStr(dwIpAddr, szIpAddr);
89135050
VK
420 if ((FindNodeByIP(zoneId, dwIpAddr) != NULL) ||
421 (FindSubnetByIP(zoneId, dwIpAddr) != NULL))
5039dede 422 {
35f836fe 423 DbgPrintf(4, _T("AcceptNewNode(%s): node already exist in database"), szIpAddr);
5039dede
AK
424 return FALSE; // Node already exist in database
425 }
426
22d657d2
VK
427 Interface *iface = GetOldNodeWithNewIP(dwIpAddr, dwNetMask, zoneId, macAddr);
428 if (iface != NULL)
baa5324c 429 {
5db0d494
VK
430 if (!HostIsReachable(dwIpAddr, zoneId, false, NULL, NULL))
431 {
432 DbgPrintf(4, _T("AcceptNewNode(%s): found existing interface with same MAC address, but new IP is not reachable"), szIpAddr);
433 return FALSE;
434 }
435
22d657d2
VK
436 Node *oldNode = iface->getParentNode();
437 if (iface->IpAddr() == oldNode->IpAddr())
438 {
439 // we should change node's primary IP only if old IP for this MAC was also node's primary IP
440 TCHAR szOldIpAddr[16];
441 IpToStr(oldNode->IpAddr(), szOldIpAddr);
442 DbgPrintf(4, _T("AcceptNewNode(%s): node already exist in database with ip %s, will change to new"),
443 szIpAddr, szOldIpAddr);
444 oldNode->changeIPAddress(dwIpAddr);
445 }
baa5324c
AK
446 return FALSE;
447 }
448
496390c2 449 // Allow filtering by loaded modules
967893bb 450 for(UINT32 i = 0; i < g_dwNumModules; i++)
496390c2
VK
451 {
452 if (g_pModuleList[i].pfAcceptNewNode != NULL)
453 {
454 if (!g_pModuleList[i].pfAcceptNewNode(dwIpAddr, dwNetMask, zoneId, macAddr))
455 return FALSE; // filtered out by module
456 }
457 }
458
5039dede 459 // Read configuration
35f836fe 460 ConfigReadStr(_T("DiscoveryFilter"), szFilter, MAX_DB_STRING, _T(""));
5039dede
AK
461 StrStrip(szFilter);
462
56e92fc9 463 // Check for filter script
35f836fe 464 if ((szFilter[0] == 0) || (!_tcsicmp(szFilter, _T("none"))))
5039dede 465 {
5db0d494
VK
466 if (!HostIsReachable(dwIpAddr, zoneId, false, NULL, NULL))
467 {
468 DbgPrintf(4, _T("AcceptNewNode(%s): host is not reachable"), szIpAddr);
469 return FALSE;
470 }
35f836fe 471 DbgPrintf(4, _T("AcceptNewNode(%s): no filtering, node accepted"), szIpAddr);
5039dede
AK
472 return TRUE; // No filtering
473 }
474
475 // Initialize new node data
476 memset(&data, 0, sizeof(DISCOVERY_FILTER_DATA));
477 data.dwIpAddr = dwIpAddr;
478 data.dwNetMask = dwNetMask;
479 data.dwSubnetAddr = dwIpAddr & dwNetMask;
480
56e92fc9 481 // Check for address range if we use simple filter instead of script
967893bb 482 UINT32 autoFilterFlags;
56e92fc9
VK
483 if (!_tcsicmp(szFilter, _T("auto")))
484 {
485 autoFilterFlags = ConfigReadULong(_T("DiscoveryFilterFlags"), DFF_ALLOW_AGENT | DFF_ALLOW_SNMP);
486 DbgPrintf(4, _T("AcceptNewNode(%s): auto filter, flags=%04X"), szIpAddr, autoFilterFlags);
487
488 if (autoFilterFlags & DFF_ONLY_RANGE)
489 {
490 DbgPrintf(4, _T("AcceptNewNode(%s): auto filter - checking range"), szIpAddr);
491 DB_RESULT hResult = DBSelect(g_hCoreDB, _T("SELECT addr_type,addr1,addr2 FROM address_lists WHERE list_type=2"));
492 if (hResult != NULL)
493 {
494 int nRows = DBGetNumRows(hResult);
495 for(int i = 0; (i < nRows) && (!bResult); i++)
496 {
497 int nType = DBGetFieldLong(hResult, i, 0);
498 if (nType == 0)
499 {
500 // Subnet
501 bResult = (data.dwIpAddr & DBGetFieldIPAddr(hResult, i, 2)) == DBGetFieldIPAddr(hResult, i, 1);
502 }
503 else
504 {
505 // Range
506 bResult = ((data.dwIpAddr >= DBGetFieldIPAddr(hResult, i, 1)) &&
507 (data.dwIpAddr <= DBGetFieldIPAddr(hResult, i, 2)));
508 }
509 }
510 DBFreeResult(hResult);
511 }
3b71dbbb
VK
512 DbgPrintf(4, _T("AcceptNewNode(%s): auto filter - range check result is %d"), szIpAddr, bResult);
513 if (!bResult)
514 return FALSE;
56e92fc9 515 }
56e92fc9
VK
516 }
517
5db0d494
VK
518 // Check if host is reachable
519 if (!HostIsReachable(dwIpAddr, zoneId, true, &pTransport, &pAgentConn))
520 {
521 DbgPrintf(4, _T("AcceptNewNode(%s): host is not reachable"), szIpAddr);
522 return FALSE;
523 }
524 if (pTransport != NULL)
5039dede
AK
525 {
526 data.dwFlags |= NNF_IS_SNMP;
5db0d494 527 data.nSNMPVersion = pTransport->getSnmpVersion();
5039dede 528 }
5db0d494 529 if (pAgentConn != NULL)
5039dede
AK
530 {
531 data.dwFlags |= NNF_IS_AGENT;
4687826e
VK
532 pAgentConn->getParameter(_T("Agent.Version"), MAX_AGENT_VERSION_LEN, data.szAgentVersion);
533 pAgentConn->getParameter(_T("System.PlatformName"), MAX_PLATFORM_NAME_LEN, data.szPlatform);
5039dede
AK
534 }
535
536 // Check if node is a router
537 if (data.dwFlags & NNF_IS_SNMP)
538 {
5d2c5741 539 if (SnmpGet(data.nSNMPVersion, pTransport,
967893bb 540 _T(".1.3.6.1.2.1.4.1.0"), NULL, 0, &dwTemp, sizeof(UINT32), 0) == SNMP_ERR_SUCCESS)
5039dede
AK
541 {
542 if (dwTemp == 1)
543 data.dwFlags |= NNF_IS_ROUTER;
544 }
545 }
546 else if (data.dwFlags & NNF_IS_AGENT)
547 {
548 // Check IP forwarding status
4687826e 549 if (pAgentConn->getParameter(_T("Net.IP.Forwarding"), 16, szBuffer) == ERR_SUCCESS)
5039dede
AK
550 {
551 if (_tcstoul(szBuffer, NULL, 10) != 0)
552 data.dwFlags |= NNF_IS_ROUTER;
553 }
554 }
555
556 // Check various SNMP device capabilities
557 if (data.dwFlags & NNF_IS_SNMP)
558 {
559 // Get SNMP OID
5d2c5741 560 SnmpGet(data.nSNMPVersion, pTransport,
f42b8099 561 _T(".1.3.6.1.2.1.1.2.0"), NULL, 0, data.szObjectId, MAX_OID_LEN * 4, 0);
5039dede
AK
562
563 // Check if node is a bridge
5d2c5741 564 if (SnmpGet(data.nSNMPVersion, pTransport,
d525c9ed 565 _T(".1.3.6.1.2.1.17.1.1.0"), NULL, 0, szBuffer, sizeof(szBuffer), 0) == SNMP_ERR_SUCCESS)
5039dede
AK
566 {
567 data.dwFlags |= NNF_IS_BRIDGE;
568 }
569
570 // Check for CDP (Cisco Discovery Protocol) support
5d2c5741 571 if (SnmpGet(data.nSNMPVersion, pTransport,
967893bb 572 _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
573 {
574 if (dwTemp == 1)
575 data.dwFlags |= NNF_IS_CDP;
576 }
577
578 // Check for SONMP (Nortel topology discovery protocol) support
5d2c5741 579 if (SnmpGet(data.nSNMPVersion, pTransport,
967893bb 580 _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
581 {
582 if (dwTemp == 1)
583 data.dwFlags |= NNF_IS_SONMP;
584 }
585
586 // Check for LLDP (Link Layer Discovery Protocol) support
5d2c5741 587 if (SnmpGet(data.nSNMPVersion, pTransport,
d525c9ed 588 _T(".1.0.8802.1.1.2.1.3.2.0"), NULL, 0, szBuffer, sizeof(szBuffer), 0) == SNMP_ERR_SUCCESS)
5039dede
AK
589 {
590 data.dwFlags |= NNF_IS_LLDP;
591 }
592 }
593
594 // Cleanup
595 delete pAgentConn;
596 delete pTransport;
597
598 // Check if we use simple filter instead of script
35f836fe 599 if (!_tcsicmp(szFilter, _T("auto")))
5039dede 600 {
56e92fc9
VK
601 bResult = FALSE;
602 if ((autoFilterFlags & (DFF_ALLOW_AGENT | DFF_ALLOW_SNMP)) == 0)
5039dede
AK
603 {
604 bResult = TRUE;
605 }
606 else
607 {
56e92fc9 608 if (autoFilterFlags & DFF_ALLOW_AGENT)
5039dede
AK
609 {
610 if (data.dwFlags & NNF_IS_AGENT)
611 bResult = TRUE;
612 }
613
56e92fc9 614 if (autoFilterFlags & DFF_ALLOW_SNMP)
5039dede
AK
615 {
616 if (data.dwFlags & NNF_IS_SNMP)
617 bResult = TRUE;
618 }
619 }
35f836fe 620 DbgPrintf(4, _T("AcceptNewNode(%s): auto filter - bResult=%d"), szIpAddr, bResult);
5039dede
AK
621 }
622 else
623 {
bb5365ed
VK
624 g_pScriptLibrary->lock();
625 pScript = g_pScriptLibrary->findScript(szFilter);
5039dede
AK
626 if (pScript != NULL)
627 {
35f836fe 628 DbgPrintf(4, _T("AcceptNewNode(%s): Running filter script %s"), szIpAddr, szFilter);
5039dede 629 pValue = new NXSL_Value(new NXSL_Object(&m_nxslDiscoveryClass, &data));
13ceb0fb 630 if (pScript->run(new NXSL_ServerEnv, 1, &pValue) == 0)
5039dede 631 {
bb5365ed 632 bResult = (pScript->getResult()->getValueAsInt32() != 0) ? TRUE : FALSE;
35f836fe 633 DbgPrintf(4, _T("AcceptNewNode(%s): Filter script result: %d"), szIpAddr, bResult);
5039dede
AK
634 }
635 else
636 {
35f836fe 637 DbgPrintf(4, _T("AcceptNewNode(%s): Filter script execution error: %s"),
bb5365ed 638 szIpAddr, pScript->getErrorText());
35f836fe 639 PostEvent(EVENT_SCRIPT_ERROR, g_dwMgmtNode, "ssd", szFilter,
bb5365ed 640 pScript->getErrorText(), 0);
5039dede
AK
641 }
642 }
643 else
644 {
35f836fe 645 DbgPrintf(4, _T("AcceptNewNode(%s): Cannot find filter script %s"), szIpAddr, szFilter);
5039dede 646 }
bb5365ed 647 g_pScriptLibrary->unlock();
5039dede
AK
648 }
649
650 return bResult;
651}
652
496390c2
VK
653/**
654 * Node poller thread (poll new nodes and put them into the database)
655 */
5039dede
AK
656THREAD_RESULT THREAD_CALL NodePoller(void *arg)
657{
658 NEW_NODE *pInfo;
659 TCHAR szIpAddr[16], szNetMask[16];
660
35f836fe 661 DbgPrintf(1, _T("Node poller started"));
5039dede 662
89135050 663 while(!IsShutdownInProgress())
5039dede
AK
664 {
665 pInfo = (NEW_NODE *)g_nodePollerQueue.GetOrBlock();
666 if (pInfo == INVALID_POINTER_VALUE)
667 break; // Shutdown indicator received
668
dc25b21c
VK
669 DbgPrintf(4, _T("NodePoller: processing node %s/%s in zone %d"),
670 IpToStr(pInfo->dwIpAddr, szIpAddr), IpToStr(pInfo->dwNetMask, szNetMask), (int)pInfo->zoneId);
e02953a4 671 if (pInfo->ignoreFilter || AcceptNewNode(pInfo->dwIpAddr, pInfo->dwNetMask, pInfo->zoneId, pInfo->bMacAddr))
07f6978d 672 {
ff6d2c5d
VK
673 ObjectTransactionStart();
674 PollNewNode(pInfo->dwIpAddr, pInfo->dwNetMask, 0, 0, 0, NULL, 0, 0, NULL, pInfo->zoneId, true, true);
675 ObjectTransactionEnd();
07f6978d 676 }
5039dede
AK
677 free(pInfo);
678 }
35f836fe 679 DbgPrintf(1, _T("Node poller thread terminated"));
5039dede
AK
680 return THREAD_OK;
681}