- New nodes added to database correctly
authorVictor Kirhenshtein <victor@netxms.org>
Wed, 15 Oct 2003 19:26:58 +0000 (19:26 +0000)
committerVictor Kirhenshtein <victor@netxms.org>
Wed, 15 Oct 2003 19:26:58 +0000 (19:26 +0000)
- Fixed bug in AddObjectToIndex()

src/server/core/nms_core.h
src/server/core/np.cpp
src/server/core/objects.cpp
src/server/core/subnet.cpp
src/server/core/tools.cpp

index 3f40813..af7754e 100644 (file)
@@ -117,6 +117,7 @@ void Main(void);
 
 void StrStrip(char *str);
 char *IpToStr(DWORD dwAddr, char *szBuffer);
+int BitsInMask(DWORD dwMask);
 
 HMODULE DLOpen(char *szModule);
 void *DLGetSymbolAddr(HMODULE hModule, char *szSymbol);
index 5b5f6d2..8b50c4e 100644 (file)
@@ -34,8 +34,6 @@ static void PollNode(DWORD dwIpAddr, DWORD dwNetMask, DWORD dwFlags)
    Subnet *pSubnet;
    DWORD dwSubnetAddr;
 
-   printf("Polling IP: %s\n", IpToStr(dwIpAddr, NULL));
-
    // Check for node existence
    if ((FindNodeByIP(dwIpAddr) != NULL) ||
        (FindSubnetByIP(dwIpAddr) != NULL))
@@ -48,29 +46,21 @@ static void PollNode(DWORD dwIpAddr, DWORD dwNetMask, DWORD dwFlags)
 
    // Find appropriate subnet
    dwSubnetAddr = dwIpAddr & dwNetMask;
-printf("Subnet: %s\n", IpToStr(dwSubnetAddr, NULL));
    pSubnet = FindSubnetByIP(dwSubnetAddr);
-printf("Subnet object: %p\n", pSubnet);
    if (pSubnet == NULL)
    {
-printf("Subnet object not found, creating new one\n");
       pSubnet = new Subnet(dwSubnetAddr, dwNetMask);
       NetObjInsert(pSubnet);
-printf("Subnet added with ID %d\n",pSubnet->Id());
    }
 
-printf("Creating interface\n");
    pInterface = new Interface(dwIpAddr, dwNetMask);
    NetObjInsert(pInterface);
-printf("Interface added with ID %d\n",pInterface->Id());
    pNode = new Node(dwIpAddr, dwFlags, 0);
    pNode->AddInterface(pInterface);
    NetObjInsert(pNode);
-printf("Node added with ID %d\n",pNode->Id());
 
    // Link node to subnet
    pSubnet->AddNode(pNode);
-printf("Finished node processing\n");
 }
 
 
index 9529130..cd8eaf9 100644 (file)
@@ -103,17 +103,22 @@ static void AddObjectToIndex(INDEX **ppIndex, DWORD *pdwIndexSize, DWORD dwKey,
             dwPos = (dwFirst + dwLast) / 2;
             if (dwPos == 0)
                dwPos++;
-            if (pIndex[dwPos].dwKey == dwKey)
-            {
-               WriteLog(MSG_DUPLICATE_KEY, EVENTLOG_ERROR_TYPE, "x", dwKey);
-               return;
-            }
             if ((pIndex[dwPos - 1].dwKey < dwKey) && (pIndex[dwPos].dwKey > dwKey))
                break;
+            if ((pIndex[dwPos].dwKey < dwKey) && (pIndex[dwPos + 1].dwKey > dwKey))
+            {
+               dwPos++;
+               break;
+            }
             if (dwKey < pIndex[dwPos].dwKey)
                dwLast = dwPos;
-            else
+            else if (dwKey > pIndex[dwPos].dwKey)
                dwFirst = dwPos;
+            else
+            {
+               WriteLog(MSG_DUPLICATE_KEY, EVENTLOG_ERROR_TYPE, "x", dwKey);
+               return;
+            }
          }
    }
 
@@ -186,29 +191,20 @@ static void DeleteObjectFromIndex(INDEX **ppIndex, DWORD *pdwIndexSize, DWORD dw
 void NetObjInsert(NetObj *pObject)
 {
    ObjectsGlobalLock();
-printf("insert: free id = %d\n", dwFreeObjectId);
    pObject->SetId(dwFreeObjectId++);
-printf("Adding object %p to index with id %d\n", pObject, pObject->Id());
    AddObjectToIndex(&g_pIndexById, &g_dwIdIndexSize, pObject->Id(), pObject);
-printf("Added\n", pObject, pObject->Id());
    switch(pObject->Type())
    {
       case OBJECT_GENERIC:
          break;
       case OBJECT_SUBNET:
-printf("Adding subnet %p to index with addr %d\n", pObject, pObject->IpAddr());
          AddObjectToIndex(&g_pSubnetIndexByAddr, &g_dwSubnetAddrIndexSize, pObject->IpAddr(), pObject);
-printf("Added\n", pObject, pObject->Id());
          break;
       case OBJECT_NODE:
-printf("Adding node %p to index with addr %d\n", pObject, pObject->IpAddr());
          AddObjectToIndex(&g_pNodeIndexByAddr, &g_dwNodeAddrIndexSize, pObject->IpAddr(), pObject);
-printf("Added\n", pObject, pObject->Id());
          break;
       case OBJECT_INTERFACE:
-printf("Adding interface %p to index with addr %d\n", pObject, pObject->IpAddr());
          AddObjectToIndex(&g_pInterfaceIndexByAddr, &g_dwInterfaceAddrIndexSize, pObject->IpAddr(), pObject);
-printf("Added\n", pObject, pObject->Id());
          break;
       default:
          WriteLog(MSG_BAD_NETOBJ_TYPE, EVENTLOG_ERROR_TYPE, "d", pObject->Type());
index 8220646..1cc7210 100644 (file)
@@ -40,8 +40,11 @@ Subnet::Subnet()
 
 Subnet::Subnet(DWORD dwAddr, DWORD dwNetMask)
 {
+   char szBuffer[32];
+
    m_dwIpAddr = dwAddr;
    m_dwIpNetMask = dwNetMask;
+   sprintf(m_szName, "%s/%d", IpToStr(dwAddr, szBuffer), BitsInMask(dwNetMask));
 }
 
 
index e2f9114..dab5d91 100644 (file)
@@ -48,6 +48,20 @@ struct ECHOREPLY
 };
 
 
+//
+// Calculate number of bits in netmask
+//
+
+int BitsInMask(DWORD dwMask)
+{
+   int bits;
+   DWORD dwTemp;
+
+   for(bits = 0, dwTemp = ntohl(dwMask); dwTemp != 0; bits++, dwTemp <<= 1);
+   return bits;
+}
+
+
 //
 // Strip whitespaces and tabs off the string
 //