From ac6927867638bf4e542cf97a58114a661d8f4818 Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Sun, 30 Apr 2017 10:21:26 -0700 Subject: [PATCH] Move helpers for address types in internals.h to their own header --- include/tins/address_range.h | 2 +- include/tins/detail/address_helpers.h | 106 ++++++++++++++++++++++++++ include/tins/internals.h | 54 ------------- src/CMakeLists.txt | 1 + src/detail/address_helpers.cpp | 78 +++++++++++++++++++ src/internals.cpp | 36 --------- src/ip_address.cpp | 1 + 7 files changed, 187 insertions(+), 91 deletions(-) create mode 100644 include/tins/detail/address_helpers.h create mode 100644 src/detail/address_helpers.cpp diff --git a/include/tins/address_range.h b/include/tins/address_range.h index fb10cf6..89a753a 100644 --- a/include/tins/address_range.h +++ b/include/tins/address_range.h @@ -33,7 +33,7 @@ #include #include #include "endianness.h" -#include "internals.h" +#include "detail/address_helpers.h" namespace Tins { /** diff --git a/include/tins/detail/address_helpers.h b/include/tins/detail/address_helpers.h new file mode 100644 index 0000000..4a8f4be --- /dev/null +++ b/include/tins/detail/address_helpers.h @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2017, Matias Fontanini + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef TINS_ADDRESS_HELPERS_H +#define TINS_ADDRESS_HELPERS_H + +#include "hw_address.h" + +/** + * \cond + */ +namespace Tins { + +class IPv4Address; +class IPv6Address; + +namespace Internals { + +template +bool increment_buffer(T& addr) { + typename T::iterator it = addr.end() - 1; + while (it >= addr.begin() && *it == 0xff) { + *it = 0; + --it; + } + // reached end + if (it < addr.begin()) { + return true; + } + (*it)++; + return false; +} + +template +bool decrement_buffer(T& addr) { + typename T::iterator it = addr.end() - 1; + while (it >= addr.begin() && *it == 0) { + *it = 0xff; + --it; + } + // reached end + if (it < addr.begin()) { + return true; + } + (*it)--; + return false; +} + +bool increment(IPv4Address& addr); +bool increment(IPv6Address& addr); +bool decrement(IPv4Address& addr); +bool decrement(IPv6Address& addr); +template +bool increment(HWAddress& addr) { + return increment_buffer(addr); +} +template +bool decrement(HWAddress& addr) { + return decrement_buffer(addr); +} + +IPv4Address last_address_from_mask(IPv4Address addr, IPv4Address mask); +IPv6Address last_address_from_mask(IPv6Address addr, const IPv6Address& mask); +template +HWAddress last_address_from_mask(HWAddress addr, const HWAddress& mask) { + typename HWAddress::iterator addr_iter = addr.begin(); + for (typename HWAddress::const_iterator it = mask.begin(); it != mask.end(); ++it, ++addr_iter) { + *addr_iter = *addr_iter | ~*it; + } + return addr; +} + +} // Internals +} // Tins + +/** + * \endcond + */ + +#endif // TINS_ADDRESS_HELPERS_H diff --git a/include/tins/internals.h b/include/tins/internals.h index 958780d..6540e1f 100644 --- a/include/tins/internals.h +++ b/include/tins/internals.h @@ -71,63 +71,9 @@ uint32_t get_padded_icmp_inner_pdu_size(const PDU* inner_pdu, uint32_t pad_align void try_parse_icmp_extensions(Memory::InputMemoryStream& stream, uint32_t payload_length, ICMPExtensionsStructure& extensions); -template -bool increment_buffer(T& addr) { - typename T::iterator it = addr.end() - 1; - while (it >= addr.begin() && *it == 0xff) { - *it = 0; - --it; - } - // reached end - if (it < addr.begin()) { - return true; - } - (*it)++; - return false; -} - -template -bool decrement_buffer(T& addr) { - typename T::iterator it = addr.end() - 1; - while (it >= addr.begin() && *it == 0) { - *it = 0xff; - --it; - } - // reached end - if (it < addr.begin()) { - return true; - } - (*it)--; - return false; -} - -bool increment(IPv4Address& addr); -bool increment(IPv6Address& addr); -bool decrement(IPv4Address& addr); -bool decrement(IPv6Address& addr); -template -bool increment(HWAddress& addr) { - return increment_buffer(addr); -} -template -bool decrement(HWAddress& addr) { - return decrement_buffer(addr); -} - // Compares sequence numbers as defined by RFC 1982. int seq_compare(uint32_t seq1, uint32_t seq2); -IPv4Address last_address_from_mask(IPv4Address addr, IPv4Address mask); -IPv6Address last_address_from_mask(IPv6Address addr, const IPv6Address& mask); -template -HWAddress last_address_from_mask(HWAddress addr, const HWAddress& mask) { - typename HWAddress::iterator addr_iter = addr.begin(); - for (typename HWAddress::const_iterator it = mask.begin(); it != mask.end(); ++it, ++addr_iter) { - *addr_iter = *addr_iter | ~*it; - } - return addr; -} - inline bool is_dot3(const uint8_t* ptr, size_t sz) { return (sz >= 13 && ptr[12] < 8); } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 04d069b..791aad7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,6 +21,7 @@ set(SOURCES stp.cpp pppoe.cpp crypto.cpp + detail/address_helpers.cpp dhcp.cpp dhcpv6.cpp dns.cpp diff --git a/src/detail/address_helpers.cpp b/src/detail/address_helpers.cpp new file mode 100644 index 0000000..2d58011 --- /dev/null +++ b/src/detail/address_helpers.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2017, Matias Fontanini + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "ip_address.h" +#include "ipv6_address.h" +#include "endianness.h" +#include "detail/address_helpers.h" + +using Tins::IPv4Address; +using Tins::IPv6Address; + +namespace Tins { +namespace Internals { + +bool increment(IPv4Address &addr) { + uint32_t addr_int = Endian::be_to_host(addr); + bool reached_end = ++addr_int == 0xffffffff; + addr = IPv4Address(Endian::be_to_host(addr_int)); + return reached_end; +} + +bool increment(IPv6Address& addr) { + return increment_buffer(addr); +} + +bool decrement(IPv4Address& addr) { + uint32_t addr_int = Endian::be_to_host(addr); + bool reached_end = --addr_int == 0; + addr = IPv4Address(Endian::be_to_host(addr_int)); + return reached_end; +} + +bool decrement(IPv6Address& addr) { + return decrement_buffer(addr); +} + +IPv4Address last_address_from_mask(IPv4Address addr, IPv4Address mask) { + uint32_t addr_int = Endian::be_to_host(addr), + mask_int = Endian::be_to_host(mask); + return IPv4Address(Endian::host_to_be(addr_int | ~mask_int)); +} + +IPv6Address last_address_from_mask(IPv6Address addr, const IPv6Address& mask) { + IPv6Address::iterator addr_iter = addr.begin(); + for (IPv6Address::const_iterator it = mask.begin(); it != mask.end(); ++it, ++addr_iter) { + *addr_iter = *addr_iter | ~*it; + } + return addr; +} + +} // Internals +} // Tins diff --git a/src/internals.cpp b/src/internals.cpp index 4a70972..f4122c6 100644 --- a/src/internals.cpp +++ b/src/internals.cpp @@ -347,28 +347,6 @@ PDU::PDUType ip_type_to_pdu_flag(Constants::IP::e flag) { }; } -bool increment(IPv4Address &addr) { - uint32_t addr_int = Endian::be_to_host(addr); - bool reached_end = ++addr_int == 0xffffffff; - addr = IPv4Address(Endian::be_to_host(addr_int)); - return reached_end; -} - -bool increment(IPv6Address& addr) { - return increment_buffer(addr); -} - -bool decrement(IPv4Address& addr) { - uint32_t addr_int = Endian::be_to_host(addr); - bool reached_end = --addr_int == 0; - addr = IPv4Address(Endian::be_to_host(addr_int)); - return reached_end; -} - -bool decrement(IPv6Address& addr) { - return decrement_buffer(addr); -} - int seq_compare(uint32_t seq1, uint32_t seq2) { // As defined by RFC 1982 - 2 ^ (SERIAL_BITS - 1) static const uint32_t seq_number_diff = 2147483648U; @@ -383,19 +361,5 @@ int seq_compare(uint32_t seq1, uint32_t seq2) { } } -IPv4Address last_address_from_mask(IPv4Address addr, IPv4Address mask) { - uint32_t addr_int = Endian::be_to_host(addr), - mask_int = Endian::be_to_host(mask); - return IPv4Address(Endian::host_to_be(addr_int | ~mask_int)); -} - -IPv6Address last_address_from_mask(IPv6Address addr, const IPv6Address& mask) { - IPv6Address::iterator addr_iter = addr.begin(); - for (IPv6Address::const_iterator it = mask.begin(); it != mask.end(); ++it, ++addr_iter) { - *addr_iter = *addr_iter | ~*it; - } - return addr; -} - } // namespace Internals } // namespace Tins diff --git a/src/ip_address.cpp b/src/ip_address.cpp index 659f736..740ab4c 100644 --- a/src/ip_address.cpp +++ b/src/ip_address.cpp @@ -39,6 +39,7 @@ #include "ip_address.h" #include "endianness.h" #include "address_range.h" +#include "exceptions.h" using std::string; using std::ostringstream;