diff --git a/include/pdu.h b/include/pdu.h
index c9d5305..d1e95da 100644
--- a/include/pdu.h
+++ b/include/pdu.h
@@ -138,6 +138,20 @@ namespace Tins {
* \return The current inner PDU. Might be 0.
*/
PDU *inner_pdu() const { return _inner_pdu; }
+
+ /**
+ * \brief Releases the inner PDU.
+ *
+ * This method makes this PDU to no longer own the inner
+ * PDU. The current inner PDU is returned, and is not
+ * 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.
*/
diff --git a/include/utils.h b/include/utils.h
index e5c1835..38f8585 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -73,20 +73,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.
diff --git a/src/dns.cpp b/src/dns.cpp
index e21c381..d7c906c 100644
--- a/src/dns.cpp
+++ b/src/dns.cpp
@@ -132,7 +132,7 @@ const uint8_t *DNS::build_resource_list(list &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(ans.size());
}
@@ -475,7 +475,7 @@ void DNS::convert_resources(const ResourcesType &lst, std::list &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 &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)
+ )
);
}
}
diff --git a/src/ipaddress.cpp b/src/ipaddress.cpp
index ea975c2..1c851f8 100644
--- a/src/ipaddress.cpp
+++ b/src/ipaddress.cpp
@@ -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;;
}
}
diff --git a/src/pdu.cpp b/src/pdu.cpp
index a397680..4dbaddc 100644
--- a/src/pdu.cpp
+++ b/src/pdu.cpp
@@ -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 buffer(size());
serialize(&buffer[0], buffer.size(), 0);
diff --git a/src/utils.cpp b/src/utils.cpp
index f710353..4725df1 100644
--- a/src/utils.cpp
+++ b/src/utils.cpp
@@ -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)
diff --git a/tests/src/utils_test.cpp b/tests/src/utils_test.cpp
index 03b9b09..63ef380 100644
--- a/tests/src/utils_test.cpp
+++ b/tests/src/utils_test.cpp
@@ -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);