nriceresolver.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. // Original authors: jib@mozilla.com, ekr@rtfm.com
  6. // Some of this code is cut-and-pasted from nICEr. Copyright is:
  7. /*
  8. Copyright (c) 2007, Adobe Systems, Incorporated
  9. All rights reserved.
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are
  12. met:
  13. * Redistributions of source code must retain the above copyright
  14. notice, this list of conditions and the following disclaimer.
  15. * Redistributions in binary form must reproduce the above copyright
  16. notice, this list of conditions and the following disclaimer in the
  17. documentation and/or other materials provided with the distribution.
  18. * Neither the name of Adobe Systems, Network Resonance nor the names of its
  19. contributors may be used to endorse or promote products derived from
  20. this software without specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include "logging.h"
  34. #include "nspr.h"
  35. #include "prnetdb.h"
  36. #include "mozilla/Assertions.h"
  37. extern "C" {
  38. #include "nr_api.h"
  39. #include "async_timer.h"
  40. #include "nr_resolver.h"
  41. #include "transport_addr.h"
  42. }
  43. #include "mozilla/net/DNS.h" // TODO(jib@mozilla.com) down here because bug 848578
  44. #include "nsThreadUtils.h"
  45. #include "nsServiceManagerUtils.h"
  46. #include "nsIDNSService.h"
  47. #include "nsIDNSListener.h"
  48. #include "nsIDNSRecord.h"
  49. #include "nsNetCID.h"
  50. #include "nsCOMPtr.h"
  51. #include "nriceresolver.h"
  52. #include "nr_socket_prsock.h"
  53. #include "mtransport/runnable_utils.h"
  54. namespace mozilla {
  55. MOZ_MTLOG_MODULE("mtransport")
  56. NrIceResolver::NrIceResolver() :
  57. vtbl_(new nr_resolver_vtbl())
  58. #ifdef DEBUG
  59. , allocated_resolvers_(0)
  60. #endif
  61. {
  62. vtbl_->destroy = &NrIceResolver::destroy;
  63. vtbl_->resolve = &NrIceResolver::resolve;
  64. vtbl_->cancel = &NrIceResolver::cancel;
  65. }
  66. NrIceResolver::~NrIceResolver() {
  67. MOZ_ASSERT(!allocated_resolvers_);
  68. delete vtbl_;
  69. }
  70. nsresult NrIceResolver::Init() {
  71. nsresult rv;
  72. sts_thread_ = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
  73. MOZ_ASSERT(NS_SUCCEEDED(rv));
  74. dns_ = do_GetService(NS_DNSSERVICE_CONTRACTID, &rv);
  75. if (NS_FAILED(rv)) {
  76. MOZ_MTLOG(ML_ERROR, "Could not acquire DNS service");
  77. }
  78. return rv;
  79. }
  80. nr_resolver *NrIceResolver::AllocateResolver() {
  81. nr_resolver *resolver;
  82. int r = nr_resolver_create_int((void *)this, vtbl_, &resolver);
  83. MOZ_ASSERT(!r);
  84. if(r) {
  85. MOZ_MTLOG(ML_ERROR, "nr_resolver_create_int failed");
  86. return nullptr;
  87. }
  88. // We must be available to allocators until they all call DestroyResolver,
  89. // because allocators may (and do) outlive the originator of NrIceResolver.
  90. AddRef();
  91. #ifdef DEBUG
  92. ++allocated_resolvers_;
  93. #endif
  94. return resolver;
  95. }
  96. void NrIceResolver::DestroyResolver() {
  97. #ifdef DEBUG
  98. --allocated_resolvers_;
  99. #endif
  100. // Undoes Addref in AllocateResolver so the NrIceResolver can be freed.
  101. Release();
  102. }
  103. int NrIceResolver::destroy(void **objp) {
  104. if (!objp || !*objp)
  105. return 0;
  106. NrIceResolver *resolver = static_cast<NrIceResolver *>(*objp);
  107. *objp = 0;
  108. resolver->DestroyResolver();
  109. return 0;
  110. }
  111. int NrIceResolver::resolve(void *obj,
  112. nr_resolver_resource *resource,
  113. int (*cb)(void *cb_arg, nr_transport_addr *addr),
  114. void *cb_arg,
  115. void **handle) {
  116. MOZ_ASSERT(obj);
  117. return static_cast<NrIceResolver *>(obj)->resolve(resource, cb, cb_arg, handle);
  118. }
  119. int NrIceResolver::resolve(nr_resolver_resource *resource,
  120. int (*cb)(void *cb_arg, nr_transport_addr *addr),
  121. void *cb_arg,
  122. void **handle) {
  123. int _status;
  124. MOZ_ASSERT(allocated_resolvers_ > 0);
  125. ASSERT_ON_THREAD(sts_thread_);
  126. RefPtr<PendingResolution> pr;
  127. uint32_t resolve_flags = 0;
  128. if (resource->transport_protocol != IPPROTO_UDP &&
  129. resource->transport_protocol != IPPROTO_TCP) {
  130. MOZ_MTLOG(ML_ERROR, "Only UDP and TCP are supported.");
  131. ABORT(R_NOT_FOUND);
  132. }
  133. pr = new PendingResolution(sts_thread_,
  134. resource->port? resource->port : 3478,
  135. resource->transport_protocol ?
  136. resource->transport_protocol :
  137. IPPROTO_UDP,
  138. cb, cb_arg);
  139. switch(resource->address_family) {
  140. case AF_INET:
  141. resolve_flags |= nsIDNSService::RESOLVE_DISABLE_IPV6;
  142. break;
  143. case AF_INET6:
  144. resolve_flags |= nsIDNSService::RESOLVE_DISABLE_IPV4;
  145. break;
  146. default:
  147. ABORT(R_BAD_ARGS);
  148. }
  149. if (NS_FAILED(dns_->AsyncResolve(nsAutoCString(resource->domain_name),
  150. resolve_flags, pr,
  151. sts_thread_, getter_AddRefs(pr->request_)))) {
  152. MOZ_MTLOG(ML_ERROR, "AsyncResolve failed.");
  153. ABORT(R_NOT_FOUND);
  154. }
  155. // Because the C API offers no "finished" method to release the handle we
  156. // return, we cannot return the request we got from AsyncResolve directly.
  157. //
  158. // Instead, we return an addref'ed reference to PendingResolution itself,
  159. // which in turn holds the request and coordinates between cancel and
  160. // OnLookupComplete to release it only once.
  161. pr.forget(handle);
  162. _status=0;
  163. abort:
  164. return _status;
  165. }
  166. nsresult NrIceResolver::PendingResolution::OnLookupComplete(
  167. nsICancelable *request, nsIDNSRecord *record, nsresult status) {
  168. ASSERT_ON_THREAD(thread_);
  169. // First check if we've been canceled. This is single-threaded on the STS
  170. // thread, but cancel() cannot guarantee this event isn't on the queue.
  171. if (request_) {
  172. nr_transport_addr *cb_addr = nullptr;
  173. nr_transport_addr ta;
  174. // TODO(jib@mozilla.com): Revisit when we do TURN.
  175. if (NS_SUCCEEDED(status)) {
  176. net::NetAddr na;
  177. if (NS_SUCCEEDED(record->GetNextAddr(port_, &na))) {
  178. MOZ_ALWAYS_TRUE (nr_netaddr_to_transport_addr(&na, &ta,
  179. transport_) == 0);
  180. cb_addr = &ta;
  181. }
  182. }
  183. cb_(cb_arg_, cb_addr);
  184. request_ = nullptr;
  185. Release();
  186. }
  187. return NS_OK;
  188. }
  189. int NrIceResolver::cancel(void *obj, void *handle) {
  190. MOZ_ALWAYS_TRUE(obj);
  191. MOZ_ASSERT(handle);
  192. ASSERT_ON_THREAD(static_cast<NrIceResolver *>(obj)->sts_thread_);
  193. return static_cast<PendingResolution *>(handle)->cancel();
  194. }
  195. int NrIceResolver::PendingResolution::cancel() {
  196. request_->Cancel (NS_ERROR_ABORT);
  197. request_ = nullptr;
  198. Release();
  199. return 0;
  200. }
  201. NS_IMPL_ISUPPORTS(NrIceResolver::PendingResolution, nsIDNSListener);
  202. } // End of namespace mozilla