]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[2238] firstAddrInPrefix(), lastAddrInPrefix() implemented
authorTomek Mrugalski <tomasz@isc.org>
Fri, 14 Sep 2012 17:11:54 +0000 (19:11 +0200)
committerTomek Mrugalski <tomasz@isc.org>
Fri, 14 Sep 2012 17:11:54 +0000 (19:11 +0200)
src/lib/util/Makefile.am
src/lib/util/addr_utilities.cc [new file with mode: 0644]
src/lib/util/addr_utilities.h [new file with mode: 0644]
src/lib/util/tests/Makefile.am

index c56e4b8703f2d6f8c309992b1f1ed669fa2be7df..d599668e6d2dca5778f6754f0d544f67a86ba90e 100644 (file)
@@ -28,6 +28,7 @@ libb10_util_la_SOURCES += encode/binary_from_base32hex.h
 libb10_util_la_SOURCES += encode/binary_from_base16.h
 libb10_util_la_SOURCES += random/qid_gen.h random/qid_gen.cc
 libb10_util_la_SOURCES += random/random_number_generator.h
+libb10_util_la_SOURCES += addr_utilities.cc addr_utilities.h
 
 EXTRA_DIST = python/pycppwrapper_util.h
 
diff --git a/src/lib/util/addr_utilities.cc b/src/lib/util/addr_utilities.cc
new file mode 100644 (file)
index 0000000..481c0e3
--- /dev/null
@@ -0,0 +1,61 @@
+// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <util/addr_utilities.h>
+
+namespace isc {
+namespace util {
+
+isc::asiolink::IOAddress firstAddrInPrefix(const isc::asiolink::IOAddress& prefix,
+                                           uint8_t len) {
+
+    static char bitMask[]= { 0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
+    uint8_t packed[16];
+
+    memcpy(packed, prefix.getAddress().to_v6().to_bytes().data(), 16);
+
+    if (len % 8 != 0) {
+        uint8_t mask = bitMask[len % 8];
+        packed[len / 8] = packed[len / 8] & mask;
+        len = (len/8 + 1) * 8;
+    }
+    for (int i = len / 8; i < 16; ++i) {
+        packed[i] = 0x0;
+    }
+
+    return (isc::asiolink::IOAddress::from_bytes(AF_INET6, packed));
+}
+
+isc::asiolink::IOAddress lastAddrInPrefix(const isc::asiolink::IOAddress& prefix,
+                                          uint8_t len) {
+
+    static char bitMask[]= { 0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
+    uint8_t packed[16];
+
+    memcpy(packed, prefix.getAddress().to_v6().to_bytes().data(), 16);
+
+    if (len % 8 != 0) {
+        uint8_t mask = bitMask[len % 8];
+        packed[len / 8] = packed[len / 8] | ~mask;
+        len = (len/8 + 1) * 8;
+    }
+    for (int i = len / 8; i < 16; ++i) {
+        packed[i] = 0xff;
+    }
+
+    return (isc::asiolink::IOAddress::from_bytes(AF_INET6, packed));
+}
+
+};
+};
diff --git a/src/lib/util/addr_utilities.h b/src/lib/util/addr_utilities.h
new file mode 100644 (file)
index 0000000..7f35326
--- /dev/null
@@ -0,0 +1,49 @@
+// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <asiolink/io_address.h>
+
+namespace isc {
+namespace util {
+
+/// This code is based on similar code from the Dibbler project. I, Tomasz Mrugalski,
+/// as a sole creater of that code hereby release it under BSD license for the benefit
+/// of the BIND10 project.
+
+/// @brief returns a first address in a given prefix
+///
+/// Example: For 2001:db8:1::deaf:beef and length /120 the function will return
+/// 2001:db8:1::dead:bee0. See also @ref lastAddrInPrefix.
+///
+/// @param prefix and address that belongs to a prefix
+/// @param len prefix length
+///
+/// @return first address from a prefix
+isc::asiolink::IOAddress firstAddrInPrefix(const isc::asiolink::IOAddress& prefix,
+                                           uint8_t len);
+
+/// @brief returns a last address in a given prefix
+///
+/// Example: For 2001:db8:1::deaf:beef and length /112 the function will return
+/// 2001:db8:1::dead:ffff. See also @ref firstAddrInPrefix.
+///
+/// @param prefix and address that belongs to a prefix
+/// @param len prefix length
+///
+/// @return first address from a prefix
+isc::asiolink::IOAddress lastAddrInPrefix(const isc::asiolink::IOAddress& prefix,
+                                          uint8_t len);
+
+};
+};
index 105322ff2f004dbf0aa5187d18fd703edbb9784d..5f3705dd9b97ca1f8a8078e91ec65b750922de00 100644 (file)
@@ -41,6 +41,7 @@ run_unittests_SOURCES += socketsession_unittest.cc
 run_unittests_SOURCES += strutil_unittest.cc
 run_unittests_SOURCES += time_utilities_unittest.cc
 run_unittests_SOURCES += range_utilities_unittest.cc
+run_unittests_SOURCES += addr_utilities_unittest.cc
 
 run_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES)
 run_unittests_LDFLAGS = $(AM_LDFLAGS) $(GTEST_LDFLAGS)
@@ -50,6 +51,7 @@ run_unittests_LDADD += $(top_builddir)/src/lib/util/io/libb10-util-io.la
 run_unittests_LDADD += \
        $(top_builddir)/src/lib/util/unittests/libutil_unittests.la
 run_unittests_LDADD += $(top_builddir)/src/lib/exceptions/libb10-exceptions.la
+run_unittests_LDADD += $(top_builddir)/src/lib/asiolink/libb10-asiolink.la
 run_unittests_LDADD += $(GTEST_LDADD)
 endif