nriceresolverfake.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 author: 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 "nspr.h"
  34. #include "prnetdb.h"
  35. #include "mozilla/Assertions.h"
  36. extern "C" {
  37. #include "nr_api.h"
  38. #include "async_timer.h"
  39. #include "nr_resolver.h"
  40. #include "transport_addr.h"
  41. }
  42. #include "nriceresolverfake.h"
  43. #include "nr_socket_prsock.h"
  44. namespace mozilla {
  45. NrIceResolverFake::NrIceResolverFake() :
  46. vtbl_(new nr_resolver_vtbl), addrs_(), delay_ms_(100),
  47. allocated_resolvers_(0) {
  48. vtbl_->destroy = &NrIceResolverFake::destroy;
  49. vtbl_->resolve = &NrIceResolverFake::resolve;
  50. vtbl_->cancel = &NrIceResolverFake::cancel;
  51. }
  52. NrIceResolverFake::~NrIceResolverFake() {
  53. MOZ_ASSERT(allocated_resolvers_ == 0);
  54. delete vtbl_;
  55. }
  56. nr_resolver *NrIceResolverFake::AllocateResolver() {
  57. nr_resolver *resolver;
  58. int r = nr_resolver_create_int((void *)this,
  59. vtbl_, &resolver);
  60. MOZ_ASSERT(!r);
  61. if(r)
  62. return nullptr;
  63. ++allocated_resolvers_;
  64. return resolver;
  65. }
  66. void NrIceResolverFake::DestroyResolver() {
  67. --allocated_resolvers_;
  68. }
  69. int NrIceResolverFake::destroy(void **objp) {
  70. if (!objp || !*objp)
  71. return 0;
  72. NrIceResolverFake *fake = static_cast<NrIceResolverFake *>(*objp);
  73. *objp = 0;
  74. fake->DestroyResolver();
  75. return 0;
  76. }
  77. int NrIceResolverFake::resolve(void *obj,
  78. nr_resolver_resource *resource,
  79. int (*cb)(void *cb_arg,
  80. nr_transport_addr *addr),
  81. void *cb_arg,
  82. void **handle) {
  83. int r,_status;
  84. MOZ_ASSERT(obj);
  85. NrIceResolverFake *fake = static_cast<NrIceResolverFake *>(obj);
  86. MOZ_ASSERT(fake->allocated_resolvers_ > 0);
  87. PendingResolution *pending =
  88. new PendingResolution(fake,
  89. resource->domain_name,
  90. resource->port ? resource->port : 3478,
  91. resource->transport_protocol ?
  92. resource->transport_protocol :
  93. IPPROTO_UDP,
  94. resource->address_family,
  95. cb, cb_arg);
  96. if ((r=NR_ASYNC_TIMER_SET(fake->delay_ms_,NrIceResolverFake::resolve_cb,
  97. (void *)pending, &pending->timer_handle_))) {
  98. delete pending;
  99. ABORT(r);
  100. }
  101. *handle = pending;
  102. _status=0;
  103. abort:
  104. return(_status);
  105. }
  106. void NrIceResolverFake::resolve_cb(NR_SOCKET s, int how, void *cb_arg) {
  107. MOZ_ASSERT(cb_arg);
  108. PendingResolution *pending = static_cast<PendingResolution *>(cb_arg);
  109. const PRNetAddr *addr=pending->resolver_->Resolve(pending->hostname_,
  110. pending->address_family_);
  111. if (addr) {
  112. nr_transport_addr transport_addr;
  113. int r = nr_praddr_to_transport_addr(addr, &transport_addr,
  114. pending->transport_, 0);
  115. MOZ_ASSERT(!r);
  116. if (r)
  117. goto abort;
  118. r=nr_transport_addr_set_port(&transport_addr, pending->port_);
  119. MOZ_ASSERT(!r);
  120. if (r)
  121. goto abort;
  122. /* Fill in the address string */
  123. r=nr_transport_addr_fmt_addr_string(&transport_addr);
  124. MOZ_ASSERT(!r);
  125. if (r)
  126. goto abort;
  127. pending->cb_(pending->cb_arg_, &transport_addr);
  128. delete pending;
  129. return;
  130. }
  131. abort:
  132. // Resolution failed.
  133. pending->cb_(pending->cb_arg_, nullptr);
  134. delete pending;
  135. }
  136. int NrIceResolverFake::cancel(void *obj, void *handle) {
  137. MOZ_ASSERT(obj);
  138. MOZ_ASSERT(static_cast<NrIceResolverFake *>(obj)->allocated_resolvers_ > 0);
  139. PendingResolution *pending = static_cast<PendingResolution *>(handle);
  140. NR_async_timer_cancel(pending->timer_handle_);
  141. delete pending;
  142. return(0);
  143. }
  144. } // End of namespace mozilla