mirror of
https://github.com/mfontanini/libtins
synced 2026-01-23 02:35:57 +01:00
Move helpers for address types in internals.h to their own header
This commit is contained in:
@@ -33,7 +33,7 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include "endianness.h"
|
#include "endianness.h"
|
||||||
#include "internals.h"
|
#include "detail/address_helpers.h"
|
||||||
|
|
||||||
namespace Tins {
|
namespace Tins {
|
||||||
/**
|
/**
|
||||||
|
|||||||
106
include/tins/detail/address_helpers.h
Normal file
106
include/tins/detail/address_helpers.h
Normal file
@@ -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<typename T>
|
||||||
|
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<typename T>
|
||||||
|
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<size_t n>
|
||||||
|
bool increment(HWAddress<n>& addr) {
|
||||||
|
return increment_buffer(addr);
|
||||||
|
}
|
||||||
|
template<size_t n>
|
||||||
|
bool decrement(HWAddress<n>& 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<size_t n>
|
||||||
|
HWAddress<n> last_address_from_mask(HWAddress<n> addr, const HWAddress<n>& mask) {
|
||||||
|
typename HWAddress<n>::iterator addr_iter = addr.begin();
|
||||||
|
for (typename HWAddress<n>::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
|
||||||
@@ -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,
|
void try_parse_icmp_extensions(Memory::InputMemoryStream& stream,
|
||||||
uint32_t payload_length, ICMPExtensionsStructure& extensions);
|
uint32_t payload_length, ICMPExtensionsStructure& extensions);
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
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<typename T>
|
|
||||||
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<size_t n>
|
|
||||||
bool increment(HWAddress<n>& addr) {
|
|
||||||
return increment_buffer(addr);
|
|
||||||
}
|
|
||||||
template<size_t n>
|
|
||||||
bool decrement(HWAddress<n>& addr) {
|
|
||||||
return decrement_buffer(addr);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compares sequence numbers as defined by RFC 1982.
|
// Compares sequence numbers as defined by RFC 1982.
|
||||||
int seq_compare(uint32_t seq1, uint32_t seq2);
|
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<size_t n>
|
|
||||||
HWAddress<n> last_address_from_mask(HWAddress<n> addr, const HWAddress<n>& mask) {
|
|
||||||
typename HWAddress<n>::iterator addr_iter = addr.begin();
|
|
||||||
for (typename HWAddress<n>::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) {
|
inline bool is_dot3(const uint8_t* ptr, size_t sz) {
|
||||||
return (sz >= 13 && ptr[12] < 8);
|
return (sz >= 13 && ptr[12] < 8);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ set(SOURCES
|
|||||||
stp.cpp
|
stp.cpp
|
||||||
pppoe.cpp
|
pppoe.cpp
|
||||||
crypto.cpp
|
crypto.cpp
|
||||||
|
detail/address_helpers.cpp
|
||||||
dhcp.cpp
|
dhcp.cpp
|
||||||
dhcpv6.cpp
|
dhcpv6.cpp
|
||||||
dns.cpp
|
dns.cpp
|
||||||
|
|||||||
78
src/detail/address_helpers.cpp
Normal file
78
src/detail/address_helpers.cpp
Normal file
@@ -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<uint32_t>(addr);
|
||||||
|
bool reached_end = ++addr_int == 0xffffffff;
|
||||||
|
addr = IPv4Address(Endian::be_to_host<uint32_t>(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<uint32_t>(addr);
|
||||||
|
bool reached_end = --addr_int == 0;
|
||||||
|
addr = IPv4Address(Endian::be_to_host<uint32_t>(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<uint32_t>(addr),
|
||||||
|
mask_int = Endian::be_to_host<uint32_t>(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
|
||||||
@@ -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<uint32_t>(addr);
|
|
||||||
bool reached_end = ++addr_int == 0xffffffff;
|
|
||||||
addr = IPv4Address(Endian::be_to_host<uint32_t>(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<uint32_t>(addr);
|
|
||||||
bool reached_end = --addr_int == 0;
|
|
||||||
addr = IPv4Address(Endian::be_to_host<uint32_t>(addr_int));
|
|
||||||
return reached_end;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool decrement(IPv6Address& addr) {
|
|
||||||
return decrement_buffer(addr);
|
|
||||||
}
|
|
||||||
|
|
||||||
int seq_compare(uint32_t seq1, uint32_t seq2) {
|
int seq_compare(uint32_t seq1, uint32_t seq2) {
|
||||||
// As defined by RFC 1982 - 2 ^ (SERIAL_BITS - 1)
|
// As defined by RFC 1982 - 2 ^ (SERIAL_BITS - 1)
|
||||||
static const uint32_t seq_number_diff = 2147483648U;
|
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<uint32_t>(addr),
|
|
||||||
mask_int = Endian::be_to_host<uint32_t>(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 Internals
|
||||||
} // namespace Tins
|
} // namespace Tins
|
||||||
|
|||||||
@@ -39,6 +39,7 @@
|
|||||||
#include "ip_address.h"
|
#include "ip_address.h"
|
||||||
#include "endianness.h"
|
#include "endianness.h"
|
||||||
#include "address_range.h"
|
#include "address_range.h"
|
||||||
|
#include "exceptions.h"
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
using std::ostringstream;
|
using std::ostringstream;
|
||||||
|
|||||||
Reference in New Issue
Block a user