1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

Move functions to parse /proc/net/routes into utils.cpp

This commit is contained in:
Matias Fontanini
2017-04-30 10:13:58 -07:00
parent ac797a836e
commit c072ffe421
3 changed files with 52 additions and 54 deletions

View File

@@ -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,

View File

@@ -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,

View File

@@ -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<string> ifaces;