1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 // Original authors: jib@mozilla.com, ekr@rtfm.com
9 // Some of this code is cut-and-pasted from nICEr. Copyright is:
12 Copyright (c) 2007, Adobe Systems, Incorporated
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are
19 * Redistributions of source code must retain the above copyright
20 notice, this list of conditions and the following disclaimer.
22 * Redistributions in binary form must reproduce the above copyright
23 notice, this list of conditions and the following disclaimer in the
24 documentation and/or other materials provided with the distribution.
26 * Neither the name of Adobe Systems, Network Resonance nor the names of its
27 contributors may be used to endorse or promote products derived from
28 this software without specific prior written permission.
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 #include "mozilla/Assertions.h"
51 #include "async_timer.h"
52 #include "nr_resolver.h"
53 #include "transport_addr.h"
56 #include "mozilla/net/DNS.h" // TODO(jib@mozilla.com) down here because bug 848578
57 #include "nsThreadUtils.h"
58 #include "nsServiceManagerUtils.h"
59 #include "nsIDNSService.h"
60 #include "nsIDNSListener.h"
61 #include "nsIDNSRecord.h"
64 #include "nriceresolver.h"
65 #include "nr_socket_prsock.h"
66 #include "mtransport/runnable_utils.h"
70 MOZ_MTLOG_MODULE("mtransport")
72 NrIceResolver::NrIceResolver()
73 : vtbl_(new nr_resolver_vtbl())
76 allocated_resolvers_(0)
79 vtbl_
->destroy
= &NrIceResolver::destroy
;
80 vtbl_
->resolve
= &NrIceResolver::resolve
;
81 vtbl_
->cancel
= &NrIceResolver::cancel
;
84 NrIceResolver::~NrIceResolver() {
85 MOZ_ASSERT(!allocated_resolvers_
);
89 nsresult
NrIceResolver::Init() {
92 sts_thread_
= do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID
, &rv
);
93 MOZ_ASSERT(NS_SUCCEEDED(rv
));
94 dns_
= do_GetService(NS_DNSSERVICE_CONTRACTID
, &rv
);
96 MOZ_MTLOG(ML_ERROR
, "Could not acquire DNS service");
101 nr_resolver
* NrIceResolver::AllocateResolver() {
102 nr_resolver
* resolver
;
104 int r
= nr_resolver_create_int((void*)this, vtbl_
, &resolver
);
107 MOZ_MTLOG(ML_ERROR
, "nr_resolver_create_int failed");
110 // We must be available to allocators until they all call DestroyResolver,
111 // because allocators may (and do) outlive the originator of NrIceResolver.
114 ++allocated_resolvers_
;
119 void NrIceResolver::DestroyResolver() {
121 --allocated_resolvers_
;
123 // Undoes Addref in AllocateResolver so the NrIceResolver can be freed.
127 int NrIceResolver::destroy(void** objp
) {
128 if (!objp
|| !*objp
) return 0;
129 NrIceResolver
* resolver
= static_cast<NrIceResolver
*>(*objp
);
131 resolver
->DestroyResolver();
135 int NrIceResolver::resolve(void* obj
, nr_resolver_resource
* resource
,
136 int (*cb
)(void* cb_arg
, nr_transport_addr
* addr
),
137 void* cb_arg
, void** handle
) {
139 return static_cast<NrIceResolver
*>(obj
)->resolve(resource
, cb
, cb_arg
,
143 int NrIceResolver::resolve(nr_resolver_resource
* resource
,
144 int (*cb
)(void* cb_arg
, nr_transport_addr
* addr
),
145 void* cb_arg
, void** handle
) {
147 MOZ_ASSERT(allocated_resolvers_
> 0);
148 ASSERT_ON_THREAD(sts_thread_
);
149 RefPtr
<PendingResolution
> pr
;
150 uint32_t resolve_flags
= 0;
151 OriginAttributes attrs
;
153 if (resource
->transport_protocol
!= IPPROTO_UDP
&&
154 resource
->transport_protocol
!= IPPROTO_TCP
) {
155 MOZ_MTLOG(ML_ERROR
, "Only UDP and TCP are supported.");
158 pr
= new PendingResolution(
159 sts_thread_
, resource
->port
? resource
->port
: 3478,
160 resource
->transport_protocol
? resource
->transport_protocol
: IPPROTO_UDP
,
163 switch (resource
->address_family
) {
165 resolve_flags
|= nsIDNSService::RESOLVE_DISABLE_IPV6
;
168 resolve_flags
|= nsIDNSService::RESOLVE_DISABLE_IPV4
;
174 if (NS_FAILED(dns_
->AsyncResolveNative(nsAutoCString(resource
->domain_name
),
175 resolve_flags
, pr
, sts_thread_
, attrs
,
176 getter_AddRefs(pr
->request_
)))) {
177 MOZ_MTLOG(ML_ERROR
, "AsyncResolve failed.");
180 // Because the C API offers no "finished" method to release the handle we
181 // return, we cannot return the request we got from AsyncResolve directly.
183 // Instead, we return an addref'ed reference to PendingResolution itself,
184 // which in turn holds the request and coordinates between cancel and
185 // OnLookupComplete to release it only once.
193 nsresult
NrIceResolver::PendingResolution::OnLookupComplete(
194 nsICancelable
* request
, nsIDNSRecord
* record
, nsresult status
) {
195 ASSERT_ON_THREAD(thread_
);
196 // First check if we've been canceled. This is single-threaded on the STS
197 // thread, but cancel() cannot guarantee this event isn't on the queue.
199 nr_transport_addr
* cb_addr
= nullptr;
200 nr_transport_addr ta
;
201 // TODO(jib@mozilla.com): Revisit when we do TURN.
202 if (NS_SUCCEEDED(status
)) {
204 if (NS_SUCCEEDED(record
->GetNextAddr(port_
, &na
))) {
205 MOZ_ALWAYS_TRUE(nr_netaddr_to_transport_addr(&na
, &ta
, transport_
) ==
210 cb_(cb_arg_
, cb_addr
);
217 int NrIceResolver::cancel(void* obj
, void* handle
) {
218 MOZ_ALWAYS_TRUE(obj
);
220 ASSERT_ON_THREAD(static_cast<NrIceResolver
*>(obj
)->sts_thread_
);
221 return static_cast<PendingResolution
*>(handle
)->cancel();
224 int NrIceResolver::PendingResolution::cancel() {
225 request_
->Cancel(NS_ERROR_ABORT
);
231 NS_IMPL_ISUPPORTS(NrIceResolver::PendingResolution
, nsIDNSListener
);
232 } // End of namespace mozilla