3
* Copyright 2008, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#include "talk/base/nethelpers.h"
30
#include "talk/base/byteorder.h"
31
#include "talk/base/signalthread.h"
35
#if defined(LINUX) || defined(ANDROID)
36
static const size_t kInitHostentLen = 1024;
37
static const size_t kMaxHostentLen = kInitHostentLen * 8;
42
AsyncResolver::AsyncResolver() : result_(NULL), error_(0) {
45
AsyncResolver::~AsyncResolver() {
49
void AsyncResolver::DoWork() {
50
result_ = SafeGetHostByName(addr_.hostname().c_str(), &error_);
53
void AsyncResolver::OnWorkDone() {
55
addr_.SetIP(NetworkToHost32(
56
*reinterpret_cast<uint32*>(result_->h_addr_list[0])));
60
#if defined(WIN32) || defined(ANDROID) || defined(OPENBSD)
61
static hostent* DeepCopyHostent(const hostent* ent) {
62
// Get the total number of bytes we need to copy, and allocate our buffer.
63
int num_aliases = 0, num_addrs = 0;
64
size_t total_len = sizeof(hostent);
65
total_len += strlen(ent->h_name) + 1;
66
while (ent->h_aliases[num_aliases]) {
67
total_len += sizeof(char*) + strlen(ent->h_aliases[num_aliases]) + 1;
70
total_len += sizeof(char*);
71
while (ent->h_addr_list[num_addrs]) {
72
total_len += sizeof(char*) + ent->h_length;
75
total_len += sizeof(char*);
77
hostent* result = static_cast<hostent*>(malloc(total_len));
81
char* p = reinterpret_cast<char*>(result) + sizeof(hostent);
83
// Copy the hostent into it, along with its embedded pointers.
85
memcpy(p, ent->h_name, strlen(ent->h_name) + 1);
86
p += strlen(ent->h_name) + 1;
88
result->h_aliases = reinterpret_cast<char**>(p);
89
p += (num_aliases + 1) * sizeof(char*);
90
for (int i = 0; i < num_aliases; ++i) {
91
result->h_aliases[i] = p;
92
memcpy(p, ent->h_aliases[i], strlen(ent->h_aliases[i]) + 1);
93
p += strlen(ent->h_aliases[i]) + 1;
95
result->h_aliases[num_aliases] = NULL;
97
result->h_addrtype = ent->h_addrtype;
98
result->h_length = ent->h_length;
100
result->h_addr_list = reinterpret_cast<char**>(p);
101
p += (num_addrs + 1) * sizeof(char*);
102
for (int i = 0; i < num_addrs; ++i) {
103
result->h_addr_list[i] = p;
104
memcpy(p, ent->h_addr_list[i], ent->h_length);
107
result->h_addr_list[num_addrs] = NULL;
113
// The functions below are used to do gethostbyname, but with an allocated
114
// instead of a static buffer.
115
hostent* SafeGetHostByName(const char* hostname, int* herrno) {
116
if (NULL == hostname || NULL == herrno) {
119
hostent* result = NULL;
121
// On Windows we have to allocate a buffer, and manually copy the hostent,
122
// along with its embedded pointers.
123
hostent* ent = gethostbyname(hostname);
125
*herrno = WSAGetLastError();
128
result = DeepCopyHostent(ent);
130
#elif defined(LINUX) || defined(ANDROID)
131
// gethostbyname() is not thread safe, so we need to call gethostbyname_r()
132
// which is a reentrant version of gethostbyname().
133
ASSERT(kInitHostentLen > sizeof(hostent));
134
size_t size = kInitHostentLen;
136
void* buf = malloc(size);
140
char* aux = static_cast<char*>(buf) + sizeof(hostent);
141
size_t aux_len = size - sizeof(hostent);
142
while ((ret = gethostbyname_r(hostname, reinterpret_cast<hostent*>(buf), aux,
143
aux_len, &result, herrno)) == ERANGE) {
145
if (size > kMaxHostentLen) {
146
break; // Just to be safe.
148
buf = realloc(buf, size);
152
aux = static_cast<char*>(buf) + sizeof(hostent);
153
aux_len = size - sizeof(hostent);
155
if (ret != 0 || buf != result) {
160
// Note that Android's version of gethostbyname_r has a bug such that the
161
// returned hostent contains pointers into thread-local storage. (See bug
162
// 4383723.) So we deep copy the result before returning.
163
hostent* deep_copy = DeepCopyHostent(result);
168
#elif defined(OSX) || defined(IOS) || defined(FREEBSD)
169
// Mac OS returns an object with everything allocated.
170
result = getipnodebyname(hostname, AF_INET, AI_DEFAULT, herrno);
171
#elif defined(OPENBSD)
172
hostent* ent = gethostbyname(hostname);
176
result = DeepCopyHostent(ent);
179
#error "I don't know how to do gethostbyname safely on your system."
184
// This function should mirror the above function, and free any resources
185
// allocated by the above.
186
void FreeHostEnt(hostent* host) {
187
#if defined(OSX) || defined(IOS) || defined(FREEBSD)
189
#elif defined(WIN32) || defined(POSIX)
192
#error "I don't know how to free a hostent on your system."
196
const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) {
198
return win32_inet_ntop(af, src, dst, size);
200
return ::inet_ntop(af, src, dst, size);
204
int inet_pton(int af, const char* src, void *dst) {
206
return win32_inet_pton(af, src, dst);
208
return ::inet_pton(af, src, dst);
212
} // namespace talk_base