blob: a6c77f327a21203bc53eca9928d7fb33591f04a8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
/*
* Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#include <Swiften/Network/HostAddress.h>
#include <cassert>
#include <cstring>
#include <string>
#include <Swiften/Base/Log.h>
static boost::asio::ip::address localhost4 = boost::asio::ip::address(boost::asio::ip::address_v4::loopback());
static boost::asio::ip::address localhost6 = boost::asio::ip::address(boost::asio::ip::address_v6::loopback());
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() << std::endl;
}
}
HostAddress::HostAddress(const unsigned char* address, size_t length) {
assert(length == 4 || length == 16);
if (length == 4) {
boost::asio::ip::address_v4::bytes_type data;
std::memcpy(data.data(), address, length);
address_ = boost::asio::ip::address(boost::asio::ip::address_v4(data));
}
else {
boost::asio::ip::address_v6::bytes_type data;
std::memcpy(data.data(), address, length);
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 {
std::string addressString;
boost::system::error_code errorCode;
addressString = address_.to_string(errorCode);
if (errorCode) {
SWIFT_LOG(debug) << "error: " << errorCode.message() << std::endl;
}
return addressString;
}
bool HostAddress::isValid() const {
return !(address_.is_v4() && address_.to_v4().to_ulong() == 0);
}
boost::asio::ip::address HostAddress::getRawAddress() const {
return address_;
}
bool HostAddress::isLocalhost() const {
return address_ == localhost4 || address_ == localhost6;
}
}
|