• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2008 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "webrtc/base/nethelpers.h"
12 
13 #if defined(WEBRTC_WIN)
14 #include <ws2spi.h>
15 #include <ws2tcpip.h>
16 #include "webrtc/base/win32.h"
17 #endif
18 
19 #include "webrtc/base/byteorder.h"
20 #include "webrtc/base/logging.h"
21 #include "webrtc/base/signalthread.h"
22 
23 namespace rtc {
24 
ResolveHostname(const std::string & hostname,int family,std::vector<IPAddress> * addresses)25 int ResolveHostname(const std::string& hostname, int family,
26                     std::vector<IPAddress>* addresses) {
27 #ifdef __native_client__
28   ASSERT(false);
29   LOG(LS_WARNING) << "ResolveHostname() is not implemented for NaCl";
30   return -1;
31 #else  // __native_client__
32   if (!addresses) {
33     return -1;
34   }
35   addresses->clear();
36   struct addrinfo* result = NULL;
37   struct addrinfo hints = {0};
38   // TODO(djw): For now this is IPv4 only so existing users remain unaffected.
39   hints.ai_family = AF_INET;
40   hints.ai_flags = AI_ADDRCONFIG;
41   int ret = getaddrinfo(hostname.c_str(), NULL, &hints, &result);
42   if (ret != 0) {
43     return ret;
44   }
45   struct addrinfo* cursor = result;
46   for (; cursor; cursor = cursor->ai_next) {
47     if (family == AF_UNSPEC || cursor->ai_family == family) {
48       IPAddress ip;
49       if (IPFromAddrInfo(cursor, &ip)) {
50         addresses->push_back(ip);
51       }
52     }
53   }
54   freeaddrinfo(result);
55   return 0;
56 #endif  // !__native_client__
57 }
58 
59 // AsyncResolver
AsyncResolver()60 AsyncResolver::AsyncResolver() : error_(-1) {
61 }
62 
63 AsyncResolver::~AsyncResolver() = default;
64 
Start(const SocketAddress & addr)65 void AsyncResolver::Start(const SocketAddress& addr) {
66   addr_ = addr;
67   // SignalThred Start will kickoff the resolve process.
68   SignalThread::Start();
69 }
70 
GetResolvedAddress(int family,SocketAddress * addr) const71 bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const {
72   if (error_ != 0 || addresses_.empty())
73     return false;
74 
75   *addr = addr_;
76   for (size_t i = 0; i < addresses_.size(); ++i) {
77     if (family == addresses_[i].family()) {
78       addr->SetResolvedIP(addresses_[i]);
79       return true;
80     }
81   }
82   return false;
83 }
84 
GetError() const85 int AsyncResolver::GetError() const {
86   return error_;
87 }
88 
Destroy(bool wait)89 void AsyncResolver::Destroy(bool wait) {
90   SignalThread::Destroy(wait);
91 }
92 
DoWork()93 void AsyncResolver::DoWork() {
94   error_ = ResolveHostname(addr_.hostname().c_str(), addr_.family(),
95                            &addresses_);
96 }
97 
OnWorkDone()98 void AsyncResolver::OnWorkDone() {
99   SignalDone(this);
100 }
101 
inet_ntop(int af,const void * src,char * dst,socklen_t size)102 const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) {
103 #if defined(WEBRTC_WIN)
104   return win32_inet_ntop(af, src, dst, size);
105 #else
106   return ::inet_ntop(af, src, dst, size);
107 #endif
108 }
109 
inet_pton(int af,const char * src,void * dst)110 int inet_pton(int af, const char* src, void *dst) {
111 #if defined(WEBRTC_WIN)
112   return win32_inet_pton(af, src, dst);
113 #else
114   return ::inet_pton(af, src, dst);
115 #endif
116 }
117 
HasIPv6Enabled()118 bool HasIPv6Enabled() {
119 #if !defined(WEBRTC_WIN)
120   // We only need to check this for Windows XP (so far).
121   return true;
122 #else
123   if (IsWindowsVistaOrLater()) {
124     return true;
125   }
126   if (!IsWindowsXpOrLater()) {
127     return false;
128   }
129   DWORD protbuff_size = 4096;
130   scoped_ptr<char[]> protocols;
131   LPWSAPROTOCOL_INFOW protocol_infos = NULL;
132   int requested_protocols[2] = {AF_INET6, 0};
133 
134   int err = 0;
135   int ret = 0;
136   // Check for protocols in a do-while loop until we provide a buffer large
137   // enough. (WSCEnumProtocols sets protbuff_size to its desired value).
138   // It is extremely unlikely that this will loop more than once.
139   do {
140     protocols.reset(new char[protbuff_size]);
141     protocol_infos = reinterpret_cast<LPWSAPROTOCOL_INFOW>(protocols.get());
142     ret = WSCEnumProtocols(requested_protocols, protocol_infos,
143                            &protbuff_size, &err);
144   } while (ret == SOCKET_ERROR && err == WSAENOBUFS);
145 
146   if (ret == SOCKET_ERROR) {
147     return false;
148   }
149 
150   // Even if ret is positive, check specifically for IPv6.
151   // Non-IPv6 enabled WinXP will still return a RAW protocol.
152   for (int i = 0; i < ret; ++i) {
153     if (protocol_infos[i].iAddressFamily == AF_INET6) {
154       return true;
155     }
156   }
157   return false;
158 #endif
159 }
160 }  // namespace rtc
161