nriceresolver.cpp (7659B)
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/. */ 6 7 // Original authors: jib@mozilla.com, ekr@rtfm.com 8 9 // Some of this code is cut-and-pasted from nICEr. Copyright is: 10 11 /* 12 Copyright (c) 2007, Adobe Systems, Incorporated 13 All rights reserved. 14 15 Redistribution and use in source and binary forms, with or without 16 modification, are permitted provided that the following conditions are 17 met: 18 19 * Redistributions of source code must retain the above copyright 20 notice, this list of conditions and the following disclaimer. 21 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. 25 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. 29 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. 41 */ 42 43 #include "logging.h" 44 #include "mozilla/Assertions.h" 45 #include "nspr.h" 46 #include "prnetdb.h" 47 48 extern "C" { 49 // clang-format off 50 #include "nr_api.h" 51 #include "async_timer.h" 52 #include "nr_resolver.h" 53 #include "transport_addr.h" 54 // clang-format on 55 } 56 57 #include "mozilla/net/DNS.h" // TODO(jib@mozilla.com) down here because bug 848578 58 #include "nr_socket_prsock.h" 59 #include "nriceresolver.h" 60 #include "nsCOMPtr.h" 61 #include "nsIDNSListener.h" 62 #include "nsIDNSRecord.h" 63 #include "nsIDNSService.h" 64 #include "nsNetCID.h" 65 #include "nsServiceManagerUtils.h" 66 #include "nsThreadUtils.h" 67 #include "transport/runnable_utils.h" 68 69 namespace mozilla { 70 71 MOZ_MTLOG_MODULE("mtransport") 72 73 NrIceResolver::NrIceResolver() 74 : vtbl_(new nr_resolver_vtbl()) 75 #ifdef DEBUG 76 , 77 allocated_resolvers_(0) 78 #endif 79 { 80 vtbl_->destroy = &NrIceResolver::destroy; 81 vtbl_->resolve = &NrIceResolver::resolve; 82 vtbl_->cancel = &NrIceResolver::cancel; 83 } 84 85 NrIceResolver::~NrIceResolver() { 86 MOZ_ASSERT(!allocated_resolvers_); 87 delete vtbl_; 88 } 89 90 nsresult NrIceResolver::Init() { 91 nsresult rv; 92 93 sts_thread_ = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv); 94 MOZ_ASSERT(NS_SUCCEEDED(rv)); 95 dns_ = do_GetService(NS_DNSSERVICE_CONTRACTID, &rv); 96 if (NS_WARN_IF(NS_FAILED(rv))) { 97 MOZ_MTLOG(ML_ERROR, "Could not acquire DNS service"); 98 } 99 return rv; 100 } 101 102 nr_resolver* NrIceResolver::AllocateResolver() { 103 nr_resolver* resolver; 104 105 int r = nr_resolver_create_int((void*)this, vtbl_, &resolver); 106 MOZ_ASSERT(!r); 107 if (r) { 108 MOZ_MTLOG(ML_ERROR, "nr_resolver_create_int failed"); 109 return nullptr; 110 } 111 // We must be available to allocators until they all call DestroyResolver, 112 // because allocators may (and do) outlive the originator of NrIceResolver. 113 AddRef(); 114 #ifdef DEBUG 115 ++allocated_resolvers_; 116 #endif 117 return resolver; 118 } 119 120 void NrIceResolver::DestroyResolver() { 121 #ifdef DEBUG 122 --allocated_resolvers_; 123 #endif 124 // Undoes Addref in AllocateResolver so the NrIceResolver can be freed. 125 Release(); 126 } 127 128 int NrIceResolver::destroy(void** objp) { 129 if (!objp || !*objp) return 0; 130 NrIceResolver* resolver = static_cast<NrIceResolver*>(*objp); 131 *objp = nullptr; 132 resolver->DestroyResolver(); 133 return 0; 134 } 135 136 int NrIceResolver::resolve(void* obj, nr_resolver_resource* resource, 137 int (*cb)(void* cb_arg, nr_transport_addr* addr), 138 void* cb_arg, void** handle) { 139 MOZ_ASSERT(obj); 140 return static_cast<NrIceResolver*>(obj)->resolve(resource, cb, cb_arg, 141 handle); 142 } 143 144 int NrIceResolver::resolve(nr_resolver_resource* resource, 145 int (*cb)(void* cb_arg, nr_transport_addr* addr), 146 void* cb_arg, void** handle) { 147 int _status; 148 MOZ_ASSERT(allocated_resolvers_ > 0); 149 ASSERT_ON_THREAD(sts_thread_); 150 RefPtr<PendingResolution> pr; 151 nsIDNSService::DNSFlags resolve_flags = nsIDNSService::RESOLVE_DEFAULT_FLAGS; 152 OriginAttributes attrs; 153 154 if (resource->transport_protocol != IPPROTO_UDP && 155 resource->transport_protocol != IPPROTO_TCP) { 156 MOZ_MTLOG(ML_ERROR, "Only UDP and TCP are supported."); 157 ABORT(R_NOT_FOUND); 158 } 159 pr = new PendingResolution( 160 sts_thread_, resource->port ? resource->port : 3478, 161 resource->transport_protocol ? resource->transport_protocol : IPPROTO_UDP, 162 cb, cb_arg); 163 164 switch (resource->address_family) { 165 case AF_INET: 166 resolve_flags = nsIDNSService::RESOLVE_DISABLE_IPV6; 167 break; 168 case AF_INET6: 169 resolve_flags = nsIDNSService::RESOLVE_DISABLE_IPV4; 170 break; 171 default: 172 ABORT(R_BAD_ARGS); 173 } 174 175 if (NS_FAILED(dns_->AsyncResolveNative( 176 nsAutoCString(resource->domain_name), 177 nsIDNSService::RESOLVE_TYPE_DEFAULT, resolve_flags, nullptr, pr, 178 sts_thread_, attrs, getter_AddRefs(pr->request_)))) { 179 MOZ_MTLOG(ML_ERROR, "AsyncResolve failed."); 180 ABORT(R_NOT_FOUND); 181 } 182 // Because the C API offers no "finished" method to release the handle we 183 // return, we cannot return the request we got from AsyncResolve directly. 184 // 185 // Instead, we return an addref'ed reference to PendingResolution itself, 186 // which in turn holds the request and coordinates between cancel and 187 // OnLookupComplete to release it only once. 188 pr.forget(handle); 189 190 _status = 0; 191 abort: 192 return _status; 193 } 194 195 nsresult NrIceResolver::PendingResolution::OnLookupComplete( 196 nsICancelable* request, nsIDNSRecord* aRecord, nsresult status) { 197 ASSERT_ON_THREAD(thread_); 198 // First check if we've been canceled. This is single-threaded on the STS 199 // thread, but cancel() cannot guarantee this event isn't on the queue. 200 if (request_) { 201 nr_transport_addr* cb_addr = nullptr; 202 nr_transport_addr ta; 203 // TODO(jib@mozilla.com): Revisit when we do TURN. 204 if (NS_SUCCEEDED(status)) { 205 net::NetAddr na; 206 nsCOMPtr<nsIDNSAddrRecord> record = do_QueryInterface(aRecord); 207 if (record && NS_SUCCEEDED(record->GetNextAddr(port_, &na))) { 208 MOZ_ALWAYS_TRUE(nr_netaddr_to_transport_addr(&na, &ta, transport_) == 209 0); 210 cb_addr = &ta; 211 } 212 } 213 cb_(cb_arg_, cb_addr); 214 request_ = nullptr; 215 Release(); 216 } 217 return NS_OK; 218 } 219 220 int NrIceResolver::cancel(void* obj, void* handle) { 221 MOZ_ALWAYS_TRUE(obj); 222 MOZ_ASSERT(handle); 223 ASSERT_ON_THREAD(static_cast<NrIceResolver*>(obj)->sts_thread_); 224 return static_cast<PendingResolution*>(handle)->cancel(); 225 } 226 227 int NrIceResolver::PendingResolution::cancel() { 228 request_->Cancel(NS_ERROR_ABORT); 229 request_ = nullptr; 230 Release(); 231 return 0; 232 } 233 234 NS_IMPL_ISUPPORTS(NrIceResolver::PendingResolution, nsIDNSListener); 235 } // End of namespace mozilla