added InetAddress::isSubnetBroadcast
authorVictor Kirhenshtein <victor@netxms.org>
Fri, 27 Feb 2015 17:17:20 +0000 (19:17 +0200)
committerVictor Kirhenshtein <victor@netxms.org>
Fri, 27 Feb 2015 17:17:20 +0000 (19:17 +0200)
src/libnetxms/inetaddr.cpp
tests/test-libnetxms/test-libnetxms.cpp
tests/test-libnetxms/test-libnetxms.vcproj

index 1dba34e..350b3ea 100644 (file)
@@ -126,7 +126,7 @@ BYTE *InetAddress::buildHashKey(BYTE *key) const
 /**
  * Get corresponding subnet address for this InetAddress
  */
-InetAddress InetAddress::getSubnetAddress()
+InetAddress InetAddress::getSubnetAddress() const
 {
    InetAddress addr(*this);
    if ((m_family == AF_INET) && (m_maskBits < 32))
@@ -145,6 +145,33 @@ InetAddress InetAddress::getSubnetAddress()
    return addr;
 }
 
+/**
+ * Check if this address is a subnet broadcast for given subnet mask length
+ */
+bool InetAddress::isSubnetBroadcast(int maskBits) const
+{
+   if (m_family == AF_INET)
+   {
+      UINT32 mask = 0xFFFFFFFF << (32 - maskBits);
+      return (m_addr.v4 & (~mask)) == (~mask);
+   }
+   if (m_family == AF_INET6)
+   {
+      BYTE addr[16];
+      memcpy(addr, m_addr.v6, 16);
+      if (maskBits < 128)
+      {
+         int b = maskBits / 8;
+         int shift = maskBits % 8;
+         addr[b] |= ((BYTE)0xFF >> shift);
+         for(int i = b + 1; i < 16; i++)
+            addr[i] = 0xFF;
+      }
+      return !memcmp(addr, m_addr.v6, 16);
+   }
+   return false;
+}
+
 /**
  * Check if this InetAddress contain given InetAddress using current network mask
  */
index 7d44d5c..defeadd 100644 (file)
@@ -263,15 +263,41 @@ static void TestString()
    EndTest();
 }
 
+/**
+ * Test InetAddress class
+ */
+static void TestInetAddress()
+{
+   InetAddress a;
+
+   StartTest(_T("InetAddress - isSubnetBroadcast() - IPv4"));
+   a = InetAddress::parse("192.168.0.255");
+   AssertTrue(a.isSubnetBroadcast(24));
+   AssertFalse(a.isSubnetBroadcast(23));
+   EndTest();
+
+   StartTest(_T("InetAddress - isSubnetBroadcast() - IPv6"));
+   a = InetAddress::parse("fe80::ffff:ffff:ffff:ffff");
+   AssertTrue(a.isSubnetBroadcast(64));
+   AssertFalse(a.isSubnetBroadcast(63));
+   EndTest();
+}
+
 /**
  * main()
  */
 int main(int argc, char *argv[])
 {
+#ifdef _WIN32
+   WSADATA wsaData;
+       WSAStartup(MAKEWORD(2, 2), &wsaData);
+#endif
+
    TestString();
    TestStringConversion();
    TestStringMap();
    TestStringSet();
    TestMsgWaitQueue();
+   TestInetAddress();
    return 0;
 }
index 50685a8..c31e2d0 100644 (file)
@@ -64,6 +64,7 @@
                        />
                        <Tool
                                Name="VCLinkerTool"
+                               AdditionalDependencies="ws2_32.lib"
                                LinkIncremental="2"
                                GenerateDebugInformation="true"
                                SubSystem="1"
                        />
                        <Tool
                                Name="VCLinkerTool"
+                               AdditionalDependencies="ws2_32.lib"
                                LinkIncremental="2"
                                GenerateDebugInformation="true"
                                SubSystem="1"
                        />
                        <Tool
                                Name="VCLinkerTool"
+                               AdditionalDependencies="ws2_32.lib"
                                LinkIncremental="1"
                                GenerateDebugInformation="true"
                                SubSystem="1"
                        />
                        <Tool
                                Name="VCLinkerTool"
+                               AdditionalDependencies="ws2_32.lib"
                                LinkIncremental="1"
                                GenerateDebugInformation="true"
                                SubSystem="1"