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/. */
8 // Original authors: jib@mozilla.com, ekr@rtfm.com
10 // Some of this code is cut-and-pasted from nICEr. Copyright is:
13 Copyright (c) 2007, Adobe Systems, Incorporated
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions are
20 * Redistributions of source code must retain the above copyright
21 notice, this list of conditions and the following disclaimer.
23 * Redistributions in binary form must reproduce the above copyright
24 notice, this list of conditions and the following disclaimer in the
25 documentation and/or other materials provided with the distribution.
27 * Neither the name of Adobe Systems, Network Resonance nor the names of its
28 contributors may be used to endorse or promote products derived from
29 this software without specific prior written permission.
31 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 #include "mozilla/Assertions.h"
52 #include "async_timer.h"
53 #include "nr_resolver.h"
54 #include "transport_addr.h"
57 #include "mozilla/net/DNS.h" // TODO(jib@mozilla.com) down here because bug 848578
58 #include "nsThreadUtils.h"
59 #include "nsServiceManagerUtils.h"
60 #include "nsIDNSService.h"
61 #include "nsIDNSListener.h"
62 #include "nsIDNSRecord.h"
65 #include "nriceresolver.h"
66 #include "nr_socket_prsock.h"
67 #include "mtransport/runnable_utils.h"
71 MOZ_MTLOG_MODULE("mtransport")
73 NrIceResolver::NrIceResolver() :
74 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
) {
130 NrIceResolver
*resolver
= static_cast<NrIceResolver
*>(*objp
);
132 resolver
->DestroyResolver();
136 int NrIceResolver::resolve(void *obj
,
137 nr_resolver_resource
*resource
,
138 int (*cb
)(void *cb_arg
, nr_transport_addr
*addr
),
142 return static_cast<NrIceResolver
*>(obj
)->resolve(resource
, cb
, cb_arg
, handle
);
145 int NrIceResolver::resolve(nr_resolver_resource
*resource
,
146 int (*cb
)(void *cb_arg
, nr_transport_addr
*addr
),
150 MOZ_ASSERT(allocated_resolvers_
> 0);
151 ASSERT_ON_THREAD(sts_thread_
);
152 nsRefPtr
<PendingResolution
> pr
;
154 if (resource
->transport_protocol
!= IPPROTO_UDP
&&
155 resource
->transport_protocol
!= IPPROTO_TCP
) {
156 MOZ_MTLOG(ML_ERROR
, "Only UDP and TCP are is supported.");
159 pr
= new PendingResolution(sts_thread_
,
160 resource
->port
? resource
->port
: 3478,
161 resource
->transport_protocol
?
162 resource
->transport_protocol
:
165 if (NS_FAILED(dns_
->AsyncResolve(nsAutoCString(resource
->domain_name
),
166 nsIDNSService::RESOLVE_DISABLE_IPV6
, pr
,
167 sts_thread_
, getter_AddRefs(pr
->request_
)))) {
168 MOZ_MTLOG(ML_ERROR
, "AsyncResolve failed.");
171 // Because the C API offers no "finished" method to release the handle we
172 // return, we cannot return the request we got from AsyncResolve directly.
174 // Instead, we return an addref'ed reference to PendingResolution itself,
175 // which in turn holds the request and coordinates between cancel and
176 // OnLookupComplete to release it only once.
184 nsresult
NrIceResolver::PendingResolution::OnLookupComplete(
185 nsICancelable
*request
, nsIDNSRecord
*record
, nsresult status
) {
186 ASSERT_ON_THREAD(thread_
);
187 // First check if we've been canceled. This is single-threaded on the STS
188 // thread, but cancel() cannot guarantee this event isn't on the queue.
190 nr_transport_addr
*cb_addr
= nullptr;
191 nr_transport_addr ta
;
192 // TODO(jib@mozilla.com): Revisit when we do TURN.
193 if (NS_SUCCEEDED(status
)) {
195 if (NS_SUCCEEDED(record
->GetNextAddr(port_
, &na
))) {
196 MOZ_ALWAYS_TRUE (nr_netaddr_to_transport_addr(&na
, &ta
,
201 cb_(cb_arg_
, cb_addr
);
207 int NrIceResolver::cancel(void *obj
, void *handle
) {
208 MOZ_ALWAYS_TRUE(obj
);
210 ASSERT_ON_THREAD(static_cast<NrIceResolver
*>(obj
)->sts_thread_
);
211 return static_cast<PendingResolution
*>(handle
)->cancel();
214 int NrIceResolver::PendingResolution::cancel() {
215 request_
->Cancel (NS_ERROR_ABORT
);
216 canceled_
= true; // in case OnLookupComplete is already on event queue.
221 NS_IMPL_ISUPPORTS(NrIceResolver::PendingResolution
, nsIDNSListener
);
222 } // End of namespace mozilla