Bug 1578839 - Implement resizing of the browser with the embedded RDM UI. r=mtigley
[gecko.git] / media / mtransport / nriceresolver.cpp
blob4a598cac099c79498ccc8d6910ef58f082df9372
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
13 All rights reserved.
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are
17 met:
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.
43 #include "logging.h"
44 #include "nspr.h"
45 #include "prnetdb.h"
47 #include "mozilla/Assertions.h"
49 extern "C" {
50 #include "nr_api.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"
62 #include "nsNetCID.h"
63 #include "nsCOMPtr.h"
64 #include "nriceresolver.h"
65 #include "nr_socket_prsock.h"
66 #include "mtransport/runnable_utils.h"
68 namespace mozilla {
70 MOZ_MTLOG_MODULE("mtransport")
72 NrIceResolver::NrIceResolver()
73 : vtbl_(new nr_resolver_vtbl())
74 #ifdef DEBUG
76 allocated_resolvers_(0)
77 #endif
79 vtbl_->destroy = &NrIceResolver::destroy;
80 vtbl_->resolve = &NrIceResolver::resolve;
81 vtbl_->cancel = &NrIceResolver::cancel;
84 NrIceResolver::~NrIceResolver() {
85 MOZ_ASSERT(!allocated_resolvers_);
86 delete vtbl_;
89 nsresult NrIceResolver::Init() {
90 nsresult rv;
92 sts_thread_ = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
93 MOZ_ASSERT(NS_SUCCEEDED(rv));
94 dns_ = do_GetService(NS_DNSSERVICE_CONTRACTID, &rv);
95 if (NS_FAILED(rv)) {
96 MOZ_MTLOG(ML_ERROR, "Could not acquire DNS service");
98 return rv;
101 nr_resolver* NrIceResolver::AllocateResolver() {
102 nr_resolver* resolver;
104 int r = nr_resolver_create_int((void*)this, vtbl_, &resolver);
105 MOZ_ASSERT(!r);
106 if (r) {
107 MOZ_MTLOG(ML_ERROR, "nr_resolver_create_int failed");
108 return nullptr;
110 // We must be available to allocators until they all call DestroyResolver,
111 // because allocators may (and do) outlive the originator of NrIceResolver.
112 AddRef();
113 #ifdef DEBUG
114 ++allocated_resolvers_;
115 #endif
116 return resolver;
119 void NrIceResolver::DestroyResolver() {
120 #ifdef DEBUG
121 --allocated_resolvers_;
122 #endif
123 // Undoes Addref in AllocateResolver so the NrIceResolver can be freed.
124 Release();
127 int NrIceResolver::destroy(void** objp) {
128 if (!objp || !*objp) return 0;
129 NrIceResolver* resolver = static_cast<NrIceResolver*>(*objp);
130 *objp = nullptr;
131 resolver->DestroyResolver();
132 return 0;
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) {
138 MOZ_ASSERT(obj);
139 return static_cast<NrIceResolver*>(obj)->resolve(resource, cb, cb_arg,
140 handle);
143 int NrIceResolver::resolve(nr_resolver_resource* resource,
144 int (*cb)(void* cb_arg, nr_transport_addr* addr),
145 void* cb_arg, void** handle) {
146 int _status;
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.");
156 ABORT(R_NOT_FOUND);
158 pr = new PendingResolution(
159 sts_thread_, resource->port ? resource->port : 3478,
160 resource->transport_protocol ? resource->transport_protocol : IPPROTO_UDP,
161 cb, cb_arg);
163 switch (resource->address_family) {
164 case AF_INET:
165 resolve_flags |= nsIDNSService::RESOLVE_DISABLE_IPV6;
166 break;
167 case AF_INET6:
168 resolve_flags |= nsIDNSService::RESOLVE_DISABLE_IPV4;
169 break;
170 default:
171 ABORT(R_BAD_ARGS);
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.");
178 ABORT(R_NOT_FOUND);
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.
186 pr.forget(handle);
188 _status = 0;
189 abort:
190 return _status;
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.
198 if (request_) {
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)) {
203 net::NetAddr na;
204 if (NS_SUCCEEDED(record->GetNextAddr(port_, &na))) {
205 MOZ_ALWAYS_TRUE(nr_netaddr_to_transport_addr(&na, &ta, transport_) ==
207 cb_addr = &ta;
210 cb_(cb_arg_, cb_addr);
211 request_ = nullptr;
212 Release();
214 return NS_OK;
217 int NrIceResolver::cancel(void* obj, void* handle) {
218 MOZ_ALWAYS_TRUE(obj);
219 MOZ_ASSERT(handle);
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);
226 request_ = nullptr;
227 Release();
228 return 0;
231 NS_IMPL_ISUPPORTS(NrIceResolver::PendingResolution, nsIDNSListener);
232 } // End of namespace mozilla