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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
/*
* libjingle
* Copyright 2004--2005, Google Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef POSIX
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#endif
#include <cstring>
#include <sstream>
#include "talk/base/byteorder.h"
#include "talk/base/common.h"
#include "talk/base/logging.h"
#include "talk/base/socketaddress.h"
#ifdef WIN32
#undef SetPort
int inet_aton(const char * cp, struct in_addr * inp) {
inp->s_addr = inet_addr(cp);
return (inp->s_addr == INADDR_NONE) ? 0 : 1;
}
#endif // WIN32
#ifdef _DEBUG
#define DISABLE_DNS 0
#else // !_DEBUG
#define DISABLE_DNS 0
#endif // !_DEBUG
namespace talk_base {
SocketAddress::SocketAddress() {
Clear();
}
SocketAddress::SocketAddress(const std::string& hostname, int port, bool use_dns) {
SetIP(hostname, use_dns);
SetPort(port);
}
SocketAddress::SocketAddress(uint32 ip, int port) {
SetIP(ip);
SetPort(port);
}
SocketAddress::SocketAddress(const SocketAddress& addr) {
this->operator=(addr);
}
void SocketAddress::Clear() {
hostname_.clear();
ip_ = 0;
port_ = 0;
}
SocketAddress& SocketAddress::operator=(const SocketAddress& addr) {
hostname_ = addr.hostname_;
ip_ = addr.ip_;
port_ = addr.port_;
return *this;
}
void SocketAddress::SetIP(uint32 ip) {
hostname_.clear();
ip_ = ip;
}
bool SocketAddress::SetIP(const std::string& hostname, bool use_dns) {
hostname_ = hostname;
ip_ = 0;
return Resolve(true, use_dns);
}
void SocketAddress::SetResolvedIP(uint32 ip) {
ip_ = ip;
}
void SocketAddress::SetPort(int port) {
ASSERT((0 <= port) && (port < 65536));
port_ = port;
}
uint32 SocketAddress::ip() const {
return ip_;
}
uint16 SocketAddress::port() const {
return port_;
}
std::string SocketAddress::IPAsString() const {
if (!hostname_.empty())
return hostname_;
return IPToString(ip_);
}
std::string SocketAddress::PortAsString() const {
std::ostringstream ost;
ost << port_;
return ost.str();
}
std::string SocketAddress::ToString() const {
std::ostringstream ost;
ost << IPAsString();
ost << ":";
ost << port();
return ost.str();
}
bool SocketAddress::IsAny() const {
return (ip_ == 0);
}
bool SocketAddress::IsLocalIP() const {
return (ip_ >> 24) == 127;
}
bool SocketAddress::IsPrivateIP() const {
return ((ip_ >> 24) == 127) ||
((ip_ >> 24) == 10) ||
((ip_ >> 20) == ((172 << 4) | 1)) ||
((ip_ >> 16) == ((192 << 8) | 168));
}
bool SocketAddress::IsUnresolved() const {
return IsAny() && !hostname_.empty();
}
bool SocketAddress::Resolve(bool force, bool use_dns) {
if (hostname_.empty()) {
// nothing to resolve
} else if (!force && !IsAny()) {
// already resolved
} else if (uint32 ip = StringToIP(hostname_, use_dns)) {
ip_ = ip;
} else {
return false;
}
return true;
}
bool SocketAddress::operator ==(const SocketAddress& addr) const {
return EqualIPs(addr) && EqualPorts(addr);
}
bool SocketAddress::operator <(const SocketAddress& addr) const {
if (ip_ < addr.ip_)
return true;
else if (addr.ip_ < ip_)
return false;
// We only check hostnames if both IPs are zero. This matches EqualIPs()
if (addr.ip_ == 0) {
if (hostname_ < addr.hostname_)
return true;
else if (addr.hostname_ < hostname_)
return false;
}
return port_ < addr.port_;
}
bool SocketAddress::EqualIPs(const SocketAddress& addr) const {
return (ip_ == addr.ip_) && ((ip_ != 0) || (hostname_ == addr.hostname_));
}
bool SocketAddress::EqualPorts(const SocketAddress& addr) const {
return (port_ == addr.port_);
}
size_t SocketAddress::Hash() const {
size_t h = 0;
h ^= ip_;
h ^= port_ | (port_ << 16);
return h;
}
size_t SocketAddress::Size_() const {
return sizeof(ip_) + sizeof(port_);
}
void SocketAddress::Write_(char* buf, int len) const {
// TODO: Depending on how this is used, we may want/need to write hostname
ASSERT((size_t)len >= Size_());
reinterpret_cast<uint32*>(buf)[0] = ip_;
buf += sizeof(ip_);
reinterpret_cast<uint16*>(buf)[0] = port_;
}
void SocketAddress::Read_(const char* buf, int len) {
ASSERT((size_t)len >= Size_());
ip_ = reinterpret_cast<const uint32*>(buf)[0];
buf += sizeof(ip_);
port_ = reinterpret_cast<const uint16*>(buf)[0];
}
void SocketAddress::ToSockAddr(sockaddr_in* saddr) const {
memset(saddr, 0, sizeof(*saddr));
saddr->sin_family = AF_INET;
saddr->sin_port = HostToNetwork16(port_);
if (0 == ip_) {
saddr->sin_addr.s_addr = INADDR_ANY;
} else {
saddr->sin_addr.s_addr = HostToNetwork32(ip_);
}
}
void SocketAddress::FromSockAddr(const sockaddr_in& saddr) {
SetIP(NetworkToHost32(saddr.sin_addr.s_addr));
SetPort(NetworkToHost16(saddr.sin_port));
}
std::string SocketAddress::IPToString(uint32 ip) {
std::ostringstream ost;
ost << ((ip >> 24) & 0xff);
ost << '.';
ost << ((ip >> 16) & 0xff);
ost << '.';
ost << ((ip >> 8) & 0xff);
ost << '.';
ost << ((ip >> 0) & 0xff);
return ost.str();
}
uint32 SocketAddress::StringToIP(const std::string& hostname, bool use_dns) {
uint32 ip = 0;
in_addr addr;
if (inet_aton(hostname.c_str(), &addr) != 0) {
ip = NetworkToHost32(addr.s_addr);
} else if (use_dns) {
// Note: this is here so we can spot spurious DNS resolutions for a while
LOG(INFO) << "=== DNS RESOLUTION (" << hostname << ") ===";
#if DISABLE_DNS
LOG(WARNING) << "*** DNS DISABLED ***";
#if WIN32
WSASetLastError(WSAHOST_NOT_FOUND);
#endif // WIN32
#endif // DISABLE_DNS
if (hostent * pHost = gethostbyname(hostname.c_str())) {
ip = NetworkToHost32(*reinterpret_cast<uint32 *>(pHost->h_addr_list[0]));
} else {
#if WIN32
LOG(LS_ERROR) << "gethostbyname error: " << WSAGetLastError();
#else
LOG(LS_ERROR) << "gethostbyname error: " << strerror(h_errno);
#endif
}
LOG(INFO) << hostname << " resolved to " << IPToString(ip);
}
return ip;
}
std::string SocketAddress::GetHostname() {
char hostname[256];
if (gethostname(hostname, ARRAY_SIZE(hostname)) == 0)
return hostname;
return "";
}
bool SocketAddress::GetLocalIPs(std::vector<uint32>& ips) {
ips.clear();
const std::string hostname = GetHostname();
if (hostname.empty())
return false;
if (hostent * pHost = gethostbyname(hostname.c_str())) {
for (size_t i=0; pHost->h_addr_list[i]; ++i) {
uint32 ip =
NetworkToHost32(*reinterpret_cast<uint32 *>(pHost->h_addr_list[i]));
ips.push_back(ip);
}
return !ips.empty();
}
#if WIN32
LOG(LS_ERROR) << "gethostbyname error: " << WSAGetLastError();
#else
LOG(LS_ERROR) << "gethostbyname error: " << strerror(h_errno);
#endif
return false;
}
} // namespace talk_base
|