From c072ffe42154fccef4a626df2c005fa3bd1c1eff Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Sun, 30 Apr 2017 10:13:58 -0700 Subject: [PATCH] Move functions to parse /proc/net/routes into utils.cpp --- include/tins/internals.h | 4 ---- src/internals.cpp | 50 -------------------------------------- src/utils.cpp | 52 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 54 deletions(-) diff --git a/include/tins/internals.h b/include/tins/internals.h index f2d30f2..958780d 100644 --- a/include/tins/internals.h +++ b/include/tins/internals.h @@ -52,10 +52,6 @@ class ICMPExtensionsStructure; namespace Internals { -void skip_line(std::istream& input); -bool from_hex(const std::string& str, uint32_t& result); -bool from_hex(const std::string& str, std::string& result); - PDU* pdu_from_flag(Constants::Ethernet::e flag, const uint8_t* buffer, uint32_t size, bool rawpdu_on_no_match = true); PDU* pdu_from_flag(Constants::IP::e flag, const uint8_t* buffer, diff --git a/src/internals.cpp b/src/internals.cpp index 7f9b384..4a70972 100644 --- a/src/internals.cpp +++ b/src/internals.cpp @@ -64,56 +64,6 @@ using Tins::Memory::InputMemoryStream; namespace Tins { namespace Internals { -bool from_hex(const string& str, uint32_t& result) { - size_t i = 0; - result = 0; - while (i < str.size()) { - uint8_t tmp; - if (str[i] >= 'A' && str[i] <= 'F') { - tmp = (str[i] - 'A' + 10); - } - else if (str[i] >= '0' && str[i] <= '9') { - tmp = (str[i] - '0'); - } - else { - return false; - } - result = (result << 4) | tmp; - i++; - } - return true; -} - -bool from_hex(const string& str, string& result) { - result = ""; - for (size_t i = 0; i < str.size(); i+= 2) { - uint8_t value = 0; - for (size_t j = i; j < i + 2 && j < str.size(); ++j) { - if (str[j] >= 'A' && str[j] <= 'F') { - value = (value << 4) | (str[j] - 'A' + 10); - } - else if (str[j] >= 'a' && str[j] <= 'f') { - value = (value << 4) | (str[j] - 'a' + 10); - } - else if (str[j] >= '0' && str[j] <= '9') { - value = (value << 4) | (str[j] - '0'); - } - else { - return false; - } - } - result.push_back(value); - } - return true; -} - -void skip_line(std::istream& input) { - int c = 0; - while (c != '\n' && input) { - c = input.get(); - } -} - Tins::PDU* pdu_from_flag(Constants::Ethernet::e flag, const uint8_t* buffer, uint32_t size, diff --git a/src/utils.cpp b/src/utils.cpp index 3a6e0b9..8645607 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -72,6 +72,7 @@ #include "detail/smart_ptr.h" using std::string; +using std::istream; using std::set; using std::ifstream; using std::vector; @@ -82,6 +83,57 @@ using Tins::Memory::InputMemoryStream; using Tins::Memory::OutputMemoryStream; /** \cond */ + +bool from_hex(const string& str, uint32_t& result) { + size_t i = 0; + result = 0; + while (i < str.size()) { + uint8_t tmp; + if (str[i] >= 'A' && str[i] <= 'F') { + tmp = (str[i] - 'A' + 10); + } + else if (str[i] >= '0' && str[i] <= '9') { + tmp = (str[i] - '0'); + } + else { + return false; + } + result = (result << 4) | tmp; + i++; + } + return true; +} + +bool from_hex(const string& str, string& result) { + result.clear(); + for (size_t i = 0; i < str.size(); i+= 2) { + uint8_t value = 0; + for (size_t j = i; j < i + 2 && j < str.size(); ++j) { + if (str[j] >= 'A' && str[j] <= 'F') { + value = (value << 4) | (str[j] - 'A' + 10); + } + else if (str[j] >= 'a' && str[j] <= 'f') { + value = (value << 4) | (str[j] - 'a' + 10); + } + else if (str[j] >= '0' && str[j] <= '9') { + value = (value << 4) | (str[j] - '0'); + } + else { + return false; + } + } + result.push_back(value); + } + return true; +} + +void skip_line(istream& input) { + int c = 0; + while (c != '\n' && input) { + c = input.get(); + } +} + struct InterfaceCollector { set ifaces;