mirror of
https://github.com/mfontanini/libtins
synced 2026-01-25 03:31:36 +01:00
Added IP pdu class. It's compiling so far.
This commit is contained in:
38
src/utils.cpp
Normal file
38
src/utils.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
#include "utils.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
uint32_t Tins::Utils::ip_to_int(const string &ip) {
|
||||
uint32_t result(0), i(0), end, bytes_found(0);
|
||||
while(i < ip.size() && bytes_found < 4) {
|
||||
uint8_t this_byte(0);
|
||||
end = i + 3;
|
||||
while(i < ip.size() && i < end && ip[i] != '.') {
|
||||
if(ip[i] < '0' || ip[i] > '9')
|
||||
throw std::runtime_error("Non-digit character found in ip");
|
||||
this_byte = (this_byte * 10) + (ip[i] - '0');
|
||||
i++;
|
||||
}
|
||||
result = (result << 8) | this_byte;
|
||||
bytes_found++;
|
||||
if(bytes_found < 4 && i < ip.size() && ip[i] == '.')
|
||||
i++;
|
||||
}
|
||||
if(bytes_found < 4 || (i < ip.size() && bytes_found == 4))
|
||||
throw std::runtime_error("Invalid ip address");
|
||||
return result;
|
||||
}
|
||||
|
||||
string Tins::Utils::ip_to_string(uint32_t ip) {
|
||||
ostringstream oss;
|
||||
int mask(24);
|
||||
while(mask >=0) {
|
||||
oss << ((ip >> mask) & 0xff);
|
||||
if(mask)
|
||||
oss << '.';
|
||||
mask -= 8;
|
||||
}
|
||||
return oss.str();
|
||||
}
|
||||
Reference in New Issue
Block a user