summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Markmann <tm@ayena.de>2016-11-08 14:29:17 (GMT)
committerTobias Markmann <tm@ayena.de>2016-11-18 08:49:39 (GMT)
commit43479ef719ea8fc6abbf654730b47c4583140508 (patch)
treec0a05a837b8988c0875fedb6161c08f3dcb2ffb0 /Swiften/Network/HostAddress.cpp
parentc82f95fd431e702137d5f2e3dda4cf0ae424e837 (diff)
downloadswift-43479ef719ea8fc6abbf654730b47c4583140508.zip
swift-43479ef719ea8fc6abbf654730b47c4583140508.tar.bz2
Improve string to HostAddress conversion API
Previously HostAddress had a constructor which allowed initialisation via a std::string. This initialisation can fail and this is heavily used for checking whether a string is a valid IP address. This constructor is removed in this commit and replaced by a static method HostAddress::fromString, taking a string and returning an optional HostAddress. This clearly communicates that the conversion can fail. Test-Information: ./scons test=all passes on macOS 10.12.1. Change-Id: Idaafee6f84010ce541c55f267ac77ad6ac8f02b4
Diffstat (limited to 'Swiften/Network/HostAddress.cpp')
-rw-r--r--Swiften/Network/HostAddress.cpp18
1 files changed, 10 insertions, 8 deletions
diff --git a/Swiften/Network/HostAddress.cpp b/Swiften/Network/HostAddress.cpp
index 63cd3f2..6eca80b 100644
--- a/Swiften/Network/HostAddress.cpp
+++ b/Swiften/Network/HostAddress.cpp
@@ -20,14 +20,6 @@ namespace Swift {
HostAddress::HostAddress() {
}
-HostAddress::HostAddress(const std::string& address) {
- boost::system::error_code errorCode;
- address_ = boost::asio::ip::address::from_string(address, errorCode);
- if (errorCode) {
- SWIFT_LOG(warning) << "error: " << errorCode.message() << " (" << errorCode << ")" << ", " << "address: " << address << std::endl;
- }
-}
-
HostAddress::HostAddress(const unsigned char* address, size_t length) {
assert(length == 4 || length == 16);
if (length == 4) {
@@ -69,4 +61,14 @@ bool HostAddress::isLocalhost() const {
return address_ == localhost4 || address_ == localhost6;
}
+boost::optional<HostAddress> HostAddress::fromString(const std::string& addressString) {
+ boost::optional<HostAddress> hostAddress;
+ boost::system::error_code errorCode;
+ boost::asio::ip::address address = boost::asio::ip::address::from_string(addressString, errorCode);
+ if (!errorCode) {
+ hostAddress = HostAddress(address);
+ }
+ return hostAddress;
+}
+
}