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

Removed Utils::ip_to_int/string. Everything is using IPv4Address now.

This commit is contained in:
Matias Fontanini
2012-08-27 23:54:43 -03:00
parent e1a84be0cb
commit fbd6ef397b
7 changed files with 35 additions and 87 deletions

View File

@@ -139,6 +139,20 @@ namespace Tins {
*/
PDU *inner_pdu() const { return _inner_pdu; }
/**
* \brief Releases the inner PDU.
*
* This method makes this PDU to <b>no longer own</b> the inner
* PDU. The current inner PDU is returned, and is <b>not</b>
* destroyed.
*
* Use this method if you want to somehow re-use a PDU that
* is already owned by another PDU.
*
* \return The current inner PDU. Might be 0.
*/
PDU *release_inner_pdu();
/** \brief Sets the flag identifier.
*/
void flag(uint32_t new_flag);

View File

@@ -74,20 +74,6 @@ namespace Tins {
IPv4Address mask;
};
/**
* \brief Convert a dotted-ip-notation string to an integer.
*
* \param ip A dotted ip notation string
*/
uint32_t ip_to_int(const std::string &ip);
/**
* \brief Convert an integer ip to a dotted-ip-notation string.
*
* \param ip An integer ip.
*/
std::string ip_to_string(uint32_t ip);
/**
* \brief Resolves a domain name and returns its corresponding ip address.
*

View File

@@ -132,7 +132,7 @@ const uint8_t *DNS::build_resource_list(list<ResourceRecord*> &lst, const uint8_
if(contains_dname(res->info.type))
std::copy(ptr, ptr + res->data.size(), res->data.begin());
else if(res->data.size() == sizeof(uint32_t))
*(uint32_t*)&res->data[0] = Utils::be_to_host(*(uint32_t*)ptr);
*(uint32_t*)&res->data[0] = *(uint32_t*)ptr;
else
throw std::runtime_error("Not enough size for resource data");
@@ -220,7 +220,7 @@ void DNS::add_query(const Query &query) {
}
void DNS::add_answer(const string &name, QueryType type, QueryClass qclass, uint32_t ttl, IPv4Address ip) {
ResourceRecord *res = make_record(name, type, qclass, ttl, (uint32_t)ip);
ResourceRecord *res = make_record(name, type, qclass, ttl, Utils::host_to_be((uint32_t)ip));
ans.push_back(res);
dns.answers = Utils::host_to_be<uint16_t>(ans.size());
}
@@ -475,7 +475,7 @@ void DNS::convert_resources(const ResourcesType &lst, std::list<Resource> &res)
ptr = (*it)->data_pointer();
sz = (*it)->data_size();
if(sz == 4)
addr = Utils::ip_to_string(*(uint32_t*)ptr);
addr = IPv4Address(*(uint32_t*)ptr).to_string();
else {
if(Utils::be_to_host((*it)->info.type) == MX) {
ptr += 2;
@@ -485,7 +485,8 @@ void DNS::convert_resources(const ResourcesType &lst, std::list<Resource> &res)
}
res.push_back(
Resource(dname, addr, Utils::be_to_host((*it)->info.type),
Utils::host_to_be((*it)->info.qclass), Utils::be_to_host((*it)->info.ttl))
Utils::host_to_be((*it)->info.qclass), Utils::be_to_host((*it)->info.ttl)
)
);
}
}

View File

@@ -46,13 +46,7 @@ IPv4Address::operator uint32_t() const {
std::string IPv4Address::to_string() const {
std::ostringstream oss;
int mask(24);
while(mask >=0) {
oss << ((ip_addr >> mask) & 0xff);
if(mask)
oss << '.';
mask -= 8;
}
oss << *this;
return oss.str();
}
@@ -81,6 +75,14 @@ uint32_t IPv4Address::ip_to_int(const string &ip) {
}
std::ostream &operator<<(std::ostream &output, const IPv4Address &addr) {
return output << addr.to_string();
int mask(24);
uint32_t ip_addr = addr.ip_addr;
while(mask >=0) {
output << ((ip_addr >> mask) & 0xff);
if(mask)
output << '.';
mask -= 8;
}
return output;;
}
}

View File

@@ -70,6 +70,12 @@ void PDU::inner_pdu(PDU *next_pdu) {
_inner_pdu = next_pdu;
}
PDU *PDU::release_inner_pdu() {
PDU *result = 0;
std::swap(result, _inner_pdu);
return result;
}
PDU::serialization_type PDU::serialize() {
std::vector<uint8_t> buffer(size());
serialize(&buffer[0], buffer.size(), 0);

View File

@@ -93,42 +93,6 @@ void Utils::Internals::skip_line(istream &input) {
/** \endcond */
uint32_t Utils::ip_to_int(const string &ip) {
uint32_t result(0), i(0), end, bytes_found(0);
while(i < ip.size() && bytes_found < 4) {
uint16_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++;
}
if (this_byte > 0xFF) {
throw std::runtime_error("Byte greater than 255");
}
result = (result << 8) | (this_byte & 0xFF);
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 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();
}
IPv4Address Utils::resolve_ip(const string &to_resolve) {
struct hostent *data = gethostbyname(to_resolve.c_str());
if(!data)

View File

@@ -57,30 +57,6 @@ const uint8_t UtilsTest::data[] = {
};
const uint32_t UtilsTest::data_len = 500;
TEST_F(UtilsTest, IpToInt) {
EXPECT_EQ(Utils::ip_to_int("0.0.0.0"), zero_int_ip);
EXPECT_EQ(Utils::ip_to_int("255.255.255.255"), full_int_ip);
EXPECT_EQ(Utils::ip_to_int("1.2.255.3"), mix_int_ip);
/* Invalid number */
EXPECT_THROW(Utils::ip_to_int("123.a.5.6"), std::runtime_error);
EXPECT_THROW(Utils::ip_to_int("0.0.256.0"), std::runtime_error);
EXPECT_THROW(Utils::ip_to_int("0.0.255.0a"), std::runtime_error);
EXPECT_THROW(Utils::ip_to_int("0.0.255.127a"), std::runtime_error);
EXPECT_THROW(Utils::ip_to_int("0.0.255.1.5"), std::runtime_error);
}
TEST_F(UtilsTest, IpToString) {
EXPECT_EQ(Utils::ip_to_string(zero_int_ip), "0.0.0.0");
EXPECT_EQ(Utils::ip_to_string(full_int_ip), "255.255.255.255");
EXPECT_EQ(Utils::ip_to_string(mix_int_ip), "1.2.255.3");
}
TEST_F(UtilsTest, ResolveIp) {
IPv4Address localhost_ip("127.0.0.1");
@@ -89,7 +65,6 @@ TEST_F(UtilsTest, ResolveIp) {
}
TEST_F(UtilsTest, NetToHostS) {
uint16_t a = 0x01FE;
uint16_t b = Utils::net_to_host_s(a);