~ubuntu-branches/debian/experimental/kopete/experimental

« back to all changes in this revision

Viewing changes to protocols/jabber/googletalk/libjingle/talk/base/nethelpers.cc

  • Committer: Package Import Robot
  • Author(s): Maximiliano Curia
  • Date: 2015-02-24 11:32:57 UTC
  • mfrom: (1.1.41 vivid)
  • Revision ID: package-import@ubuntu.com-20150224113257-gnupg4v7lzz18ij0
Tags: 4:14.12.2-1
* New upstream release (14.12.2).
* Bump Standards-Version to 3.9.6, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * libjingle
3
 
 * Copyright 2008, Google Inc.
4
 
 *
5
 
 * Redistribution and use in source and binary forms, with or without
6
 
 * modification, are permitted provided that the following conditions are met:
7
 
 *
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.
15
 
 *
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.
26
 
 */
27
 
 
28
 
#include "talk/base/nethelpers.h"
29
 
 
30
 
#include "talk/base/byteorder.h"
31
 
#include "talk/base/signalthread.h"
32
 
 
33
 
namespace talk_base {
34
 
 
35
 
#if defined(LINUX) || defined(ANDROID)
36
 
static const size_t kInitHostentLen = 1024;
37
 
static const size_t kMaxHostentLen = kInitHostentLen * 8;
38
 
#endif
39
 
 
40
 
// AsyncResolver
41
 
 
42
 
AsyncResolver::AsyncResolver() : result_(NULL), error_(0) {
43
 
}
44
 
 
45
 
AsyncResolver::~AsyncResolver() {
46
 
  FreeHostEnt(result_);
47
 
}
48
 
 
49
 
void AsyncResolver::DoWork() {
50
 
  result_ = SafeGetHostByName(addr_.hostname().c_str(), &error_);
51
 
}
52
 
 
53
 
void AsyncResolver::OnWorkDone() {
54
 
  if (result_) {
55
 
    addr_.SetIP(NetworkToHost32(
56
 
        *reinterpret_cast<uint32*>(result_->h_addr_list[0])));
57
 
  }
58
 
}
59
 
 
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;
68
 
    ++num_aliases;
69
 
  }
70
 
  total_len += sizeof(char*);
71
 
  while (ent->h_addr_list[num_addrs]) {
72
 
    total_len += sizeof(char*) + ent->h_length;
73
 
    ++num_addrs;
74
 
  }
75
 
  total_len += sizeof(char*);
76
 
 
77
 
  hostent* result = static_cast<hostent*>(malloc(total_len));
78
 
  if (NULL == result) {
79
 
    return NULL;
80
 
  }
81
 
  char* p = reinterpret_cast<char*>(result) + sizeof(hostent);
82
 
 
83
 
  // Copy the hostent into it, along with its embedded pointers.
84
 
  result->h_name = p;
85
 
  memcpy(p, ent->h_name, strlen(ent->h_name) + 1);
86
 
  p += strlen(ent->h_name) + 1;
87
 
 
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;
94
 
  }
95
 
  result->h_aliases[num_aliases] = NULL;
96
 
 
97
 
  result->h_addrtype = ent->h_addrtype;
98
 
  result->h_length = ent->h_length;
99
 
 
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);
105
 
    p += ent->h_length;
106
 
  }
107
 
  result->h_addr_list[num_addrs] = NULL;
108
 
  
109
 
  return result;
110
 
}
111
 
#endif
112
 
 
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) {
117
 
    return NULL;
118
 
  }
119
 
  hostent* result = NULL;
120
 
#if defined(WIN32)
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);
124
 
  if (!ent) {
125
 
    *herrno = WSAGetLastError();
126
 
    return NULL;
127
 
  }
128
 
  result = DeepCopyHostent(ent);
129
 
  *herrno = 0;
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;
135
 
  int ret;
136
 
  void* buf = malloc(size);
137
 
  if (NULL == buf) {
138
 
    return NULL;
139
 
  }
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) {
144
 
    size *= 2;
145
 
    if (size > kMaxHostentLen) {
146
 
      break;  // Just to be safe.
147
 
    }
148
 
    buf = realloc(buf, size);
149
 
    if (NULL == buf) {
150
 
      return NULL;
151
 
    }
152
 
    aux = static_cast<char*>(buf) + sizeof(hostent);
153
 
    aux_len = size - sizeof(hostent);
154
 
  }
155
 
  if (ret != 0 || buf != result) {
156
 
    free(buf);
157
 
    return NULL;
158
 
  }
159
 
#if defined(ANDROID)
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);
164
 
  FreeHostEnt(result);
165
 
  result = deep_copy;
166
 
#endif
167
 
  *herrno = 0;
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);
173
 
  if (!ent) {
174
 
    return NULL;
175
 
  }
176
 
  result = DeepCopyHostent(ent);
177
 
  *herrno = 0;
178
 
#else
179
 
#error "I don't know how to do gethostbyname safely on your system."
180
 
#endif
181
 
  return result;
182
 
}
183
 
 
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)
188
 
  freehostent(host);
189
 
#elif defined(WIN32) || defined(POSIX)
190
 
  free(host);
191
 
#else
192
 
#error "I don't know how to free a hostent on your system."
193
 
#endif
194
 
}
195
 
 
196
 
const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) {
197
 
#ifdef WIN32
198
 
  return win32_inet_ntop(af, src, dst, size);
199
 
#else
200
 
  return ::inet_ntop(af, src, dst, size);
201
 
#endif
202
 
}
203
 
 
204
 
int inet_pton(int af, const char* src, void *dst) {
205
 
#ifdef WIN32
206
 
  return win32_inet_pton(af, src, dst);
207
 
#else
208
 
  return ::inet_pton(af, src, dst);
209
 
#endif
210
 
}
211
 
 
212
 
}  // namespace talk_base