Skip to content

Commit

Permalink
Remove inclusion of algorithm almost everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
mfontanini committed May 1, 2017
1 parent 82e97ad commit 60b5f3e
Show file tree
Hide file tree
Showing 19 changed files with 87 additions and 110 deletions.
10 changes: 5 additions & 5 deletions include/tins/ip.h
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ class TINS_API IP : public PDU {
* \return The stored options.
*/
const options_type& options() const {
return ip_options_;
return options_;
}

/* Setters */
Expand Down Expand Up @@ -525,7 +525,7 @@ class TINS_API IP : public PDU {
*/
void add_option(option &&opt) {
internal_add_option(opt);
ip_options_.push_back(std::move(opt));
options_.push_back(std::move(opt));
}

/**
Expand All @@ -538,8 +538,8 @@ class TINS_API IP : public PDU {
*/
template<typename... Args>
void add_option(Args&&... args) {
ip_options_.emplace_back(std::forward<Args>(args)...);
internal_add_option(ip_options_.back());
options_.emplace_back(std::forward<Args>(args)...);
internal_add_option(options_.back());
}
#endif

Expand Down Expand Up @@ -770,7 +770,7 @@ class TINS_API IP : public PDU {

ip_header header_;
uint16_t options_size_, padded_options_size_;
options_type ip_options_;
options_type options_;
};

} // Tins
Expand Down
40 changes: 27 additions & 13 deletions include/tins/pdu_option.h
Original file line number Diff line number Diff line change
Expand Up @@ -537,22 +537,36 @@ class PDUOption {
};

namespace Internals {
/*
* \cond
*/
template <typename Option>
struct option_type_equality_comparator {
option_type_equality_comparator(typename Option::option_type type) : type(type) { }
/*
* \cond
*/

bool operator()(const Option& opt) const {
return opt.option() == type;
template <typename Option, typename Container>
typename Container::iterator find_option(Container& cont, typename Option::option_type type) {
typename Container::iterator iter;
for (iter = cont.begin(); iter != cont.end(); ++iter) {
if (iter->option() == type) {
break;
}
}
return iter;
}

typename Option::option_type type;
};
/*
* \endcond
*/
template <typename Option, typename Container>
typename Container::const_iterator find_option_const(const Container& cont,
typename Option::option_type type) {
typename Container::const_iterator iter;
for (iter = cont.begin(); iter != cont.end(); ++iter) {
if (iter->option() == type) {
break;
}
}
return iter;
}

/*
* \endcond
*/
} // Internals

} // namespace Tins
Expand Down
1 change: 0 additions & 1 deletion src/arp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
*/

#include <cstring>
#include <algorithm>
#include "arp.h"
#include "ethernetII.h"
#include "rawpdu.h"
Expand Down
8 changes: 2 additions & 6 deletions src/dhcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

#include <stdexcept>
#include <cstring>
#include <algorithm>
#include "endianness.h"
#include "dhcp.h"
#include "exceptions.h"
Expand All @@ -39,7 +38,6 @@ using std::string;
using std::vector;
using std::list;
using std::runtime_error;
using std::find_if;

using Tins::Memory::InputMemoryStream;
using Tins::Memory::OutputMemoryStream;
Expand Down Expand Up @@ -113,13 +111,11 @@ const DHCP::option* DHCP::search_option(OptionTypes opt) const {
}

DHCP::options_type::const_iterator DHCP::search_option_iterator(OptionTypes opt) const {
Internals::option_type_equality_comparator<option> comparator(opt);
return find_if(options_.begin(), options_.end(), comparator);
return Internals::find_option_const<option>(options_, opt);
}

DHCP::options_type::iterator DHCP::search_option_iterator(OptionTypes opt) {
Internals::option_type_equality_comparator<option> comparator(opt);
return find_if(options_.begin(), options_.end(), comparator);
return Internals::find_option<option>(options_, opt);
}

void DHCP::type(Flags type) {
Expand Down
9 changes: 2 additions & 7 deletions src/dhcpv6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,10 @@
*/

#include <vector>
#include <algorithm>
#include "dhcpv6.h"
#include "exceptions.h"
#include "memory_helpers.h"

using std::find_if;
using std::copy;
using std::vector;
using std::runtime_error;
using std::memcpy;
Expand Down Expand Up @@ -105,13 +102,11 @@ const DHCPv6::option* DHCPv6::search_option(OptionTypes type) const {
}

DHCPv6::options_type::const_iterator DHCPv6::search_option_iterator(OptionTypes type) const {
Internals::option_type_equality_comparator<option> comparator(type);
return find_if(options_.begin(), options_.end(), comparator);
return Internals::find_option_const<option>(options_, type);
}

DHCPv6::options_type::iterator DHCPv6::search_option_iterator(OptionTypes type) {
Internals::option_type_equality_comparator<option> comparator(type);
return find_if(options_.begin(), options_.end(), comparator);
return Internals::find_option<option>(options_, type);
}

void DHCPv6::write_option(const option& opt, OutputMemoryStream& stream) const {
Expand Down
7 changes: 2 additions & 5 deletions src/dot11/dot11_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#ifdef TINS_HAVE_DOT11

#include <cstring>
#include <algorithm>
#include "macros.h"
#include "exceptions.h"

Expand Down Expand Up @@ -126,13 +125,11 @@ const Dot11::option* Dot11::search_option(OptionTypes type) const {
}

Dot11::options_type::const_iterator Dot11::search_option_iterator(OptionTypes type) const {
Internals::option_type_equality_comparator<option> comparator(static_cast<uint8_t>(type));
return find_if(options_.begin(), options_.end(), comparator);
return Internals::find_option_const<option>(options_, type);
}

Dot11::options_type::iterator Dot11::search_option_iterator(OptionTypes type) {
Internals::option_type_equality_comparator<option> comparator(static_cast<uint8_t>(type));
return find_if(options_.begin(), options_.end(), comparator);
return Internals::find_option<option>(options_, type);
}

void Dot11::protocol(small_uint<2> new_proto) {
Expand Down
3 changes: 1 addition & 2 deletions src/dot11/dot11_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include "dot11/dot11_control.h"
#ifdef TINS_HAVE_DOT11

#include <algorithm>
#include "memory_helpers.h"

using std::copy;
Expand Down Expand Up @@ -234,7 +233,7 @@ void Dot11BlockAck::fragment_number(small_uint<4> frag) {
}

void Dot11BlockAck::bitmap(const uint8_t* bit) {
copy(bit, bit + bitmap_size, bitmap_);
memcpy(bitmap_, bit, bitmap_size);
}

void Dot11BlockAck::write_ext_header(OutputMemoryStream& stream) {
Expand Down
12 changes: 5 additions & 7 deletions src/dot3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

#include <cstring>
#include <stdexcept>
#include <algorithm>
#include "macros.h"
#ifndef _WIN32
#if defined(BSD) || defined(__FreeBSD_kernel__)
Expand Down Expand Up @@ -78,11 +77,11 @@ Dot3::Dot3(const uint8_t* buffer, uint32_t total_sz) {
}

void Dot3::dst_addr(const address_type& address) {
copy(address.begin(), address.end(), header_.dst_mac);
address.copy(header_.dst_mac);
}

void Dot3::src_addr(const address_type& address) {
copy(address.begin(), address.end(), header_.src_mac);
address.copy(header_.src_mac);
}

void Dot3::length(uint16_t value) {
Expand Down Expand Up @@ -121,11 +120,10 @@ bool Dot3::matches_response(const uint8_t* ptr, uint32_t total_sz) const {
if (total_sz < sizeof(header_)) {
return false;
}
const size_t addr_sz = address_type::address_size;
const dot3_header* eth_ptr = (const dot3_header*)ptr;
if (equal(header_.src_mac, header_.src_mac + addr_sz, eth_ptr->dst_mac)) {
if (equal(header_.src_mac, header_.src_mac + addr_sz, eth_ptr->dst_mac) ||
dst_addr() == BROADCAST) {
if (address_type(header_.src_mac) == address_type(eth_ptr->dst_mac)) {
if (address_type(header_.src_mac) == address_type(eth_ptr->dst_mac) ||
dst_addr() == BROADCAST) {
ptr += sizeof(dot3_header);
total_sz -= sizeof(dot3_header);
return inner_pdu() ? inner_pdu()->matches_response(ptr, total_sz) : true;
Expand Down
24 changes: 10 additions & 14 deletions src/eapol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,11 @@

#include <cstring>
#include <stdexcept>
#include <algorithm>
#include "eapol.h"
#include "exceptions.h"
#include "rawpdu.h"
#include "memory_helpers.h"

using std::copy;
using std::min;
using std::memset;
using std::memcpy;

Expand All @@ -51,7 +48,8 @@ PDU::metadata EAPOL::extract_metadata(const uint8_t *buffer, uint32_t total_sz)
}
const eapol_header* header = (const eapol_header*)buffer;
uint32_t advertised_size = Endian::be_to_host<uint16_t>(header->length) + 4;
return metadata(min(total_sz, advertised_size), pdu_flag, PDU::UNKNOWN);
const uint32_t actual_size = (total_sz < advertised_size) ? total_sz : advertised_size;
return metadata(actual_size, pdu_flag, PDU::UNKNOWN);
}

EAPOL::EAPOL(uint8_t packet_type, EAPOLTYPE type)
Expand All @@ -73,10 +71,8 @@ EAPOL* EAPOL::from_bytes(const uint8_t* buffer, uint32_t total_sz) {
const eapol_header* ptr = (const eapol_header*)buffer;
uint32_t data_len = Endian::be_to_host<uint16_t>(ptr->length);
// at least 4 for fields always present
total_sz = min(
total_sz,
data_len + 4
);
data_len += 4;
total_sz = (total_sz < data_len) ? total_sz : data_len;
switch(ptr->type) {
case RC4:
return new Tins::RC4EAPOL(buffer, total_sz);
Expand Down Expand Up @@ -142,7 +138,7 @@ void RC4EAPOL::replay_counter(uint64_t value) {
}

void RC4EAPOL::key_iv(const uint8_t* ptr) {
copy(ptr, ptr + sizeof(header_.key_iv), header_.key_iv);
memcpy(header_.key_iv, ptr, sizeof(header_.key_iv));
}

void RC4EAPOL::key_flag(small_uint<1> flag) {
Expand Down Expand Up @@ -195,31 +191,31 @@ RSNEAPOL::RSNEAPOL(const uint8_t* buffer, uint32_t total_sz)
}

void RSNEAPOL::nonce(const uint8_t* ptr) {
copy(ptr, ptr + nonce_size, header_.nonce);
memcpy(header_.nonce, ptr, nonce_size);
}

void RSNEAPOL::rsc(const uint8_t* ptr) {
copy(ptr, ptr + rsc_size, header_.rsc);
memcpy(header_.rsc, ptr, rsc_size);
}

void RSNEAPOL::id(const uint8_t* ptr) {
copy(ptr, ptr + id_size, header_.id);
memcpy(header_.id, ptr, id_size);
}

void RSNEAPOL::replay_counter(uint64_t new_replay_counter) {
header_.replay_counter = Endian::host_to_be(new_replay_counter);
}

void RSNEAPOL::mic(const uint8_t* ptr) {
copy(ptr, ptr + mic_size, header_.mic);
memcpy(header_.mic, ptr, mic_size);
}

void RSNEAPOL::wpa_length(uint16_t length) {
header_.wpa_length = Endian::host_to_be(length);
}

void RSNEAPOL::key_iv(const uint8_t* ptr) {
copy(ptr, ptr + sizeof(header_.key_iv), header_.key_iv);
memcpy(header_.key_iv, ptr, sizeof(header_.key_iv));
}

void RSNEAPOL::key_length(uint16_t length) {
Expand Down
8 changes: 2 additions & 6 deletions src/ethernetII.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
*/

#include <cstring>
#include <algorithm>
#include "macros.h"
#ifndef _WIN32
#if defined(BSD) || defined(__FreeBSD_kernel__)
Expand All @@ -48,8 +47,6 @@
#include "memory_helpers.h"
#include "detail/pdu_helpers.h"

using std::equal;

using Tins::Memory::InputMemoryStream;
using Tins::Memory::OutputMemoryStream;

Expand Down Expand Up @@ -144,10 +141,9 @@ bool EthernetII::matches_response(const uint8_t* ptr, uint32_t total_sz) const {
if (total_sz < sizeof(header_)) {
return false;
}
const size_t addr_sz = address_type::address_size;
const ethernet_header* eth_ptr = (const ethernet_header*)ptr;
if (equal(header_.src_mac, header_.src_mac + addr_sz, eth_ptr->dst_mac)) {
if (equal(header_.src_mac, header_.src_mac + addr_sz, eth_ptr->dst_mac) ||
if (address_type(header_.src_mac) == address_type(eth_ptr->dst_mac)) {
if (address_type(header_.src_mac) == address_type(eth_ptr->dst_mac) ||
!dst_addr().is_unicast()) {
return inner_pdu() ?
inner_pdu()->matches_response(ptr + sizeof(header_), total_sz - sizeof(header_)) :
Expand Down
1 change: 0 additions & 1 deletion src/icmp_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
*
*/

#include <algorithm>
#include <cstring>
#include "icmp_extension.h"
#include "exceptions.h"
Expand Down
Loading

0 comments on commit 60b5f3e

Please sign in to comment.