1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/mtransport/nriceresolverfake.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,190 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 + 1.11 +// Original author: ekr@rtfm.com 1.12 + 1.13 +// Some of this code is cut-and-pasted from nICEr. Copyright is: 1.14 + 1.15 +/* 1.16 +Copyright (c) 2007, Adobe Systems, Incorporated 1.17 +All rights reserved. 1.18 + 1.19 +Redistribution and use in source and binary forms, with or without 1.20 +modification, are permitted provided that the following conditions are 1.21 +met: 1.22 + 1.23 +* Redistributions of source code must retain the above copyright 1.24 + notice, this list of conditions and the following disclaimer. 1.25 + 1.26 +* Redistributions in binary form must reproduce the above copyright 1.27 + notice, this list of conditions and the following disclaimer in the 1.28 + documentation and/or other materials provided with the distribution. 1.29 + 1.30 +* Neither the name of Adobe Systems, Network Resonance nor the names of its 1.31 + contributors may be used to endorse or promote products derived from 1.32 + this software without specific prior written permission. 1.33 + 1.34 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.35 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.36 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.37 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.38 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.39 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.40 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.41 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.42 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.43 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.44 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.45 +*/ 1.46 + 1.47 +#include "nspr.h" 1.48 +#include "prnetdb.h" 1.49 + 1.50 +#include "mozilla/Assertions.h" 1.51 + 1.52 +extern "C" { 1.53 +#include "nr_api.h" 1.54 +#include "async_timer.h" 1.55 +#include "nr_resolver.h" 1.56 +#include "transport_addr.h" 1.57 +} 1.58 + 1.59 +#include "nriceresolverfake.h" 1.60 +#include "nr_socket_prsock.h" 1.61 + 1.62 +namespace mozilla { 1.63 + 1.64 +NrIceResolverFake::NrIceResolverFake() : 1.65 + vtbl_(new nr_resolver_vtbl), addrs_(), delay_ms_(100), 1.66 + allocated_resolvers_(0) { 1.67 + vtbl_->destroy = &NrIceResolverFake::destroy; 1.68 + vtbl_->resolve = &NrIceResolverFake::resolve; 1.69 + vtbl_->cancel = &NrIceResolverFake::cancel; 1.70 +} 1.71 + 1.72 +NrIceResolverFake::~NrIceResolverFake() { 1.73 + MOZ_ASSERT(allocated_resolvers_ == 0); 1.74 + delete vtbl_; 1.75 +} 1.76 + 1.77 + 1.78 +nr_resolver *NrIceResolverFake::AllocateResolver() { 1.79 + nr_resolver *resolver; 1.80 + 1.81 + int r = nr_resolver_create_int((void *)this, 1.82 + vtbl_, &resolver); 1.83 + MOZ_ASSERT(!r); 1.84 + if(r) 1.85 + return nullptr; 1.86 + 1.87 + ++allocated_resolvers_; 1.88 + 1.89 + return resolver; 1.90 +} 1.91 + 1.92 +void NrIceResolverFake::DestroyResolver() { 1.93 + --allocated_resolvers_; 1.94 +} 1.95 + 1.96 +int NrIceResolverFake::destroy(void **objp) { 1.97 + if (!objp || !*objp) 1.98 + return 0; 1.99 + 1.100 + NrIceResolverFake *fake = static_cast<NrIceResolverFake *>(*objp); 1.101 + *objp = 0; 1.102 + 1.103 + fake->DestroyResolver(); 1.104 + 1.105 + return 0; 1.106 +} 1.107 + 1.108 +int NrIceResolverFake::resolve(void *obj, 1.109 + nr_resolver_resource *resource, 1.110 + int (*cb)(void *cb_arg, 1.111 + nr_transport_addr *addr), 1.112 + void *cb_arg, 1.113 + void **handle) { 1.114 + int r,_status; 1.115 + 1.116 + MOZ_ASSERT(obj); 1.117 + NrIceResolverFake *fake = static_cast<NrIceResolverFake *>(obj); 1.118 + 1.119 + MOZ_ASSERT(fake->allocated_resolvers_ > 0); 1.120 + 1.121 + PendingResolution *pending = 1.122 + new PendingResolution(fake, 1.123 + resource->domain_name, 1.124 + resource->port ? resource->port : 3478, 1.125 + resource->transport_protocol ? 1.126 + resource->transport_protocol : 1.127 + IPPROTO_UDP, 1.128 + cb, cb_arg); 1.129 + 1.130 + if ((r=NR_ASYNC_TIMER_SET(fake->delay_ms_,NrIceResolverFake::resolve_cb, 1.131 + (void *)pending, &pending->timer_handle_))) { 1.132 + delete pending; 1.133 + ABORT(r); 1.134 + } 1.135 + *handle = pending; 1.136 + 1.137 + _status=0; 1.138 +abort: 1.139 + return(_status); 1.140 +} 1.141 + 1.142 +void NrIceResolverFake::resolve_cb(NR_SOCKET s, int how, void *cb_arg) { 1.143 + MOZ_ASSERT(cb_arg); 1.144 + PendingResolution *pending = static_cast<PendingResolution *>(cb_arg); 1.145 + 1.146 + const PRNetAddr *addr=pending->resolver_->Resolve(pending->hostname_); 1.147 + 1.148 + if (addr) { 1.149 + nr_transport_addr transport_addr; 1.150 + 1.151 + int r = nr_praddr_to_transport_addr(addr, &transport_addr, 1.152 + pending->transport_, 0); 1.153 + MOZ_ASSERT(!r); 1.154 + if (r) 1.155 + goto abort; 1.156 + 1.157 + r=nr_transport_addr_set_port(&transport_addr, pending->port_); 1.158 + MOZ_ASSERT(!r); 1.159 + if (r) 1.160 + goto abort; 1.161 + 1.162 + /* Fill in the address string */ 1.163 + r=nr_transport_addr_fmt_addr_string(&transport_addr); 1.164 + MOZ_ASSERT(!r); 1.165 + if (r) 1.166 + goto abort; 1.167 + 1.168 + pending->cb_(pending->cb_arg_, &transport_addr); 1.169 + delete pending; 1.170 + return; 1.171 + } 1.172 + 1.173 +abort: 1.174 + // Resolution failed. 1.175 + pending->cb_(pending->cb_arg_, nullptr); 1.176 + 1.177 + delete pending; 1.178 +} 1.179 + 1.180 +int NrIceResolverFake::cancel(void *obj, void *handle) { 1.181 + MOZ_ASSERT(obj); 1.182 + MOZ_ASSERT(static_cast<NrIceResolverFake *>(obj)->allocated_resolvers_ > 0); 1.183 + 1.184 + PendingResolution *pending = static_cast<PendingResolution *>(handle); 1.185 + 1.186 + NR_async_timer_cancel(pending->timer_handle_); 1.187 + delete pending; 1.188 + 1.189 + return(0); 1.190 +} 1.191 + 1.192 + 1.193 +} // End of namespace mozilla