1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "remoting/client/plugin/pepper_address_resolver.h"
6
7 #include "base/logging.h"
8 #include "ppapi/cpp/net_address.h"
9 #include "remoting/client/plugin/pepper_util.h"
10
11 namespace remoting {
12
PepperAddressResolver(const pp::InstanceHandle & instance)13 PepperAddressResolver::PepperAddressResolver(const pp::InstanceHandle& instance)
14 : resolver_(instance),
15 error_(0),
16 callback_factory_(this) {
17 }
18
~PepperAddressResolver()19 PepperAddressResolver::~PepperAddressResolver() {
20 }
21
Start(const rtc::SocketAddress & address)22 void PepperAddressResolver::Start(const rtc::SocketAddress& address) {
23 DCHECK(!address.hostname().empty());
24
25 PP_HostResolver_Hint hint;
26 hint.flags = 0;
27 hint.family = PP_NETADDRESS_FAMILY_UNSPECIFIED;
28 pp::CompletionCallback callback =
29 callback_factory_.NewCallback(&PepperAddressResolver::OnResolved);
30 int result = resolver_.Resolve(
31 address.hostname().c_str(), address.port(), hint, callback);
32 DCHECK_EQ(result, PP_OK_COMPLETIONPENDING);
33 }
34
GetResolvedAddress(int family,rtc::SocketAddress * addr) const35 bool PepperAddressResolver::GetResolvedAddress(int family,
36 rtc::SocketAddress* addr) const {
37 rtc::SocketAddress result;
38 switch (family) {
39 case AF_INET:
40 result = ipv4_address_;
41 break;
42 case AF_INET6:
43 result = ipv6_address_;
44 break;
45 }
46
47 if (result.IsNil())
48 return false;
49
50 *addr = result;
51 return true;
52 }
53
GetError() const54 int PepperAddressResolver::GetError() const {
55 return error_;
56 }
57
Destroy(bool wait)58 void PepperAddressResolver::Destroy(bool wait) {
59 delete this;
60 }
61
OnResolved(int32_t result)62 void PepperAddressResolver::OnResolved(int32_t result) {
63 if (result < 0) {
64 error_ = EAI_FAIL;
65 SignalDone(this);
66 return;
67 }
68
69 int count = resolver_.GetNetAddressCount();
70 for (int i = 0; i < count; ++i) {
71 pp::NetAddress address = resolver_.GetNetAddress(i);
72 if (address.GetFamily() == PP_NETADDRESS_FAMILY_IPV4 &&
73 ipv4_address_.IsNil()) {
74 PpNetAddressToSocketAddress(address, &ipv4_address_);
75 } else if (address.GetFamily() == PP_NETADDRESS_FAMILY_IPV6 &&
76 ipv6_address_.IsNil()) {
77 PpNetAddressToSocketAddress(address, &ipv6_address_);
78 }
79 }
80
81 SignalDone(this);
82 }
83
84 } // namespace remoting
85