nriceresolverfake.cpp (5307B)
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 author: 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 "mozilla/Assertions.h" 44 #include "prio.h" 45 46 extern "C" { 47 // clang-format off 48 #include "async_wait.h" 49 #include "async_timer.h" 50 #include "nr_resolver.h" 51 #include "r_macros.h" 52 #include "transport_addr.h" 53 // clang-format on 54 } 55 56 #include "nr_socket_prsock.h" 57 #include "nriceresolverfake.h" 58 59 namespace mozilla { 60 61 NrIceResolverFake::NrIceResolverFake() 62 : vtbl_(new nr_resolver_vtbl), delay_ms_(100), allocated_resolvers_(0) { 63 vtbl_->destroy = &NrIceResolverFake::destroy; 64 vtbl_->resolve = &NrIceResolverFake::resolve; 65 vtbl_->cancel = &NrIceResolverFake::cancel; 66 } 67 68 NrIceResolverFake::~NrIceResolverFake() { 69 MOZ_ASSERT(allocated_resolvers_ == 0); 70 delete vtbl_; 71 } 72 73 nr_resolver* NrIceResolverFake::AllocateResolver() { 74 nr_resolver* resolver; 75 76 int r = nr_resolver_create_int((void*)this, vtbl_, &resolver); 77 MOZ_ASSERT(!r); 78 if (r) return nullptr; 79 80 ++allocated_resolvers_; 81 82 return resolver; 83 } 84 85 void NrIceResolverFake::DestroyResolver() { --allocated_resolvers_; } 86 87 int NrIceResolverFake::destroy(void** objp) { 88 if (!objp || !*objp) return 0; 89 90 NrIceResolverFake* fake = static_cast<NrIceResolverFake*>(*objp); 91 *objp = nullptr; 92 93 fake->DestroyResolver(); 94 95 return 0; 96 } 97 98 int NrIceResolverFake::resolve(void* obj, nr_resolver_resource* resource, 99 int (*cb)(void* cb_arg, nr_transport_addr* addr), 100 void* cb_arg, void** handle) { 101 int r, _status; 102 103 MOZ_ASSERT(obj); 104 NrIceResolverFake* fake = static_cast<NrIceResolverFake*>(obj); 105 106 MOZ_ASSERT(fake->allocated_resolvers_ > 0); 107 108 PendingResolution* pending = new PendingResolution( 109 fake, resource->domain_name, resource->port ? resource->port : 3478, 110 resource->transport_protocol ? resource->transport_protocol : IPPROTO_UDP, 111 resource->address_family, cb, cb_arg); 112 113 if ((r = NR_ASYNC_TIMER_SET(fake->delay_ms_, NrIceResolverFake::resolve_cb, 114 (void*)pending, &pending->timer_handle_))) { 115 delete pending; 116 ABORT(r); 117 } 118 *handle = pending; 119 120 _status = 0; 121 abort: 122 return (_status); 123 } 124 125 void NrIceResolverFake::resolve_cb(NR_SOCKET s, int how, void* cb_arg) { 126 MOZ_ASSERT(cb_arg); 127 PendingResolution* pending = static_cast<PendingResolution*>(cb_arg); 128 129 const PRNetAddr* addr = 130 pending->resolver_->Resolve(pending->hostname_, pending->address_family_); 131 132 if (addr) { 133 nr_transport_addr transport_addr; 134 135 int r = nr_praddr_to_transport_addr(addr, &transport_addr, 136 pending->transport_, 0); 137 MOZ_ASSERT(!r); 138 if (r) goto abort; 139 140 r = nr_transport_addr_set_port(&transport_addr, pending->port_); 141 MOZ_ASSERT(!r); 142 if (r) goto abort; 143 144 /* Fill in the address string */ 145 r = nr_transport_addr_fmt_addr_string(&transport_addr); 146 MOZ_ASSERT(!r); 147 if (r) goto abort; 148 149 pending->cb_(pending->cb_arg_, &transport_addr); 150 delete pending; 151 return; 152 } 153 154 abort: 155 // Resolution failed. 156 pending->cb_(pending->cb_arg_, nullptr); 157 158 delete pending; 159 } 160 161 int NrIceResolverFake::cancel(void* obj, void* handle) { 162 MOZ_ASSERT(obj); 163 MOZ_ASSERT(static_cast<NrIceResolverFake*>(obj)->allocated_resolvers_ > 0); 164 165 PendingResolution* pending = static_cast<PendingResolution*>(handle); 166 167 NR_async_timer_cancel(pending->timer_handle_); 168 delete pending; 169 170 return (0); 171 } 172 173 } // End of namespace mozilla