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