From: Tomek Mrugalski Date: Fri, 14 Sep 2012 17:11:54 +0000 (+0200) Subject: [2238] firstAddrInPrefix(), lastAddrInPrefix() implemented X-Git-Tag: trac2402_base~5^2~17^2~11 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a66bc5d487b4b095bb9e322e33ed31c55d8af339;p=thirdparty%2Fkea.git [2238] firstAddrInPrefix(), lastAddrInPrefix() implemented --- diff --git a/src/lib/util/Makefile.am b/src/lib/util/Makefile.am index c56e4b8703..d599668e6d 100644 --- a/src/lib/util/Makefile.am +++ b/src/lib/util/Makefile.am @@ -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 index 0000000000..481c0e3c8e --- /dev/null +++ b/src/lib/util/addr_utilities.cc @@ -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 + +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 index 0000000000..7f353269d9 --- /dev/null +++ b/src/lib/util/addr_utilities.h @@ -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 + +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); + +}; +}; diff --git a/src/lib/util/tests/Makefile.am b/src/lib/util/tests/Makefile.am index 105322ff2f..5f3705dd9b 100644 --- a/src/lib/util/tests/Makefile.am +++ b/src/lib/util/tests/Makefile.am @@ -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