summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2010-10-17 12:13:36 (GMT)
committerRemko Tronçon <git@el-tramo.be>2010-10-21 18:25:00 (GMT)
commit1b58ef2af54456004390a0888c3edf104e3baa99 (patch)
treedbe4ae29de1b765a88ea704dfaa1c03af4b196b3 /Swiften/Network/HostAddress.cpp
parent07402c4e3451f2084a1c3ddc5bacfb38a66899a7 (diff)
downloadswift-contrib-1b58ef2af54456004390a0888c3edf104e3baa99.zip
swift-contrib-1b58ef2af54456004390a0888c3edf104e3baa99.tar.bz2
Added beginnings of outgoing file transfer to Swiften.
Diffstat (limited to 'Swiften/Network/HostAddress.cpp')
-rw-r--r--Swiften/Network/HostAddress.cpp61
1 files changed, 27 insertions, 34 deletions
diff --git a/Swiften/Network/HostAddress.cpp b/Swiften/Network/HostAddress.cpp
index e299429..b3876c0 100644
--- a/Swiften/Network/HostAddress.cpp
+++ b/Swiften/Network/HostAddress.cpp
@@ -9,8 +9,8 @@
#include <boost/numeric/conversion/cast.hpp>
#include <boost/lexical_cast.hpp>
#include <cassert>
-#include <sstream>
-#include <iomanip>
+#include <stdexcept>
+#include <boost/array.hpp>
#include "Swiften/Base/foreach.h"
#include "Swiften/Base/String.h"
@@ -18,50 +18,43 @@
namespace Swift {
HostAddress::HostAddress() {
- for (int i = 0; i < 4; ++i) {
- address_.push_back(0);
- }
}
HostAddress::HostAddress(const String& address) {
- std::vector<String> components = address.split('.');
- assert(components.size() == 4);
- foreach(const String& component, components) {
- address_.push_back(boost::lexical_cast<int>(component.getUTF8String()));
+ try {
+ address_ = boost::asio::ip::address::from_string(address.getUTF8String());
+ }
+ catch (const std::exception& t) {
}
}
HostAddress::HostAddress(const unsigned char* address, int length) {
assert(length == 4 || length == 16);
- address_.reserve(length);
- for (int i = 0; i < length; ++i) {
- address_.push_back(address[i]);
- }
-}
-
-std::string HostAddress::toString() const {
- if (address_.size() == 4) {
- std::ostringstream result;
- for (size_t i = 0; i < address_.size() - 1; ++i) {
- result << boost::numeric_cast<unsigned int>(address_[i]) << ".";
- }
- result << boost::numeric_cast<unsigned int>(address_[address_.size() - 1]);
- return result.str();
- }
- else if (address_.size() == 16) {
- std::ostringstream result;
- result << std::hex;
- result.fill('0');
- for (size_t i = 0; i < (address_.size() / 2) - 1; ++i) {
- result << std::setw(2) << boost::numeric_cast<unsigned int>(address_[2*i]) << std::setw(2) << boost::numeric_cast<unsigned int>(address_[(2*i)+1]) << ":";
+ if (length == 4) {
+ boost::array<unsigned char, 4> data;
+ for (int i = 0; i < length; ++i) {
+ data[i] = address[i];
}
- result << std::setw(2) << boost::numeric_cast<unsigned int>(address_[address_.size() - 2]) << std::setw(2) << boost::numeric_cast<unsigned int>(address_[address_.size() - 1]);
- return result.str();
+ address_ = boost::asio::ip::address(boost::asio::ip::address_v4(data));
}
else {
- assert(false);
- return "";
+ boost::array<unsigned char, 16> data;
+ for (int i = 0; i < length; ++i) {
+ data[i] = address[i];
+ }
+ address_ = boost::asio::ip::address(boost::asio::ip::address_v6(data));
}
}
+HostAddress::HostAddress(const boost::asio::ip::address& address) : address_(address) {
+}
+
+std::string HostAddress::toString() const {
+ return address_.to_string();
+}
+
+bool HostAddress::isValid() const {
+ return !(address_.is_v4() && address_.to_v4().to_ulong() == 0);
+}
+
}