Line data Source code
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 :
8 : // Original author: ekr@rtfm.com
9 :
10 : // Some of this code is cut-and-pasted from nICEr. Copyright is:
11 :
12 : /*
13 : Copyright (c) 2007, Adobe Systems, Incorporated
14 : All rights reserved.
15 :
16 : Redistribution and use in source and binary forms, with or without
17 : modification, are permitted provided that the following conditions are
18 : met:
19 :
20 : * Redistributions of source code must retain the above copyright
21 : notice, this list of conditions and the following disclaimer.
22 :
23 : * Redistributions in binary form must reproduce the above copyright
24 : notice, this list of conditions and the following disclaimer in the
25 : documentation and/or other materials provided with the distribution.
26 :
27 : * Neither the name of Adobe Systems, Network Resonance nor the names of its
28 : contributors may be used to endorse or promote products derived from
29 : this software without specific prior written permission.
30 :
31 : THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 : "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 : LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 : A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 : OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 : SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 : LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 : DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 : THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 : (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 : OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 : */
43 :
44 : #include "nspr.h"
45 : #include "prnetdb.h"
46 :
47 : #include "mozilla/Assertions.h"
48 :
49 : extern "C" {
50 : #include "nr_api.h"
51 : #include "async_timer.h"
52 : #include "nr_resolver.h"
53 : #include "transport_addr.h"
54 : }
55 :
56 : #include "nriceresolverfake.h"
57 : #include "nr_socket_prsock.h"
58 :
59 : namespace mozilla {
60 :
61 0 : NrIceResolverFake::NrIceResolverFake() :
62 0 : vtbl_(new nr_resolver_vtbl), addrs_(), delay_ms_(100),
63 0 : allocated_resolvers_(0) {
64 0 : vtbl_->destroy = &NrIceResolverFake::destroy;
65 0 : vtbl_->resolve = &NrIceResolverFake::resolve;
66 0 : vtbl_->cancel = &NrIceResolverFake::cancel;
67 0 : }
68 :
69 0 : NrIceResolverFake::~NrIceResolverFake() {
70 0 : MOZ_ASSERT(allocated_resolvers_ == 0);
71 0 : delete vtbl_;
72 0 : }
73 :
74 :
75 0 : nr_resolver *NrIceResolverFake::AllocateResolver() {
76 : nr_resolver *resolver;
77 :
78 0 : int r = nr_resolver_create_int((void *)this,
79 0 : vtbl_, &resolver);
80 0 : MOZ_ASSERT(!r);
81 0 : if(r)
82 0 : return nullptr;
83 :
84 0 : ++allocated_resolvers_;
85 :
86 0 : return resolver;
87 : }
88 :
89 0 : void NrIceResolverFake::DestroyResolver() {
90 0 : --allocated_resolvers_;
91 0 : }
92 :
93 0 : int NrIceResolverFake::destroy(void **objp) {
94 0 : if (!objp || !*objp)
95 0 : return 0;
96 :
97 0 : NrIceResolverFake *fake = static_cast<NrIceResolverFake *>(*objp);
98 0 : *objp = nullptr;
99 :
100 0 : fake->DestroyResolver();
101 :
102 0 : return 0;
103 : }
104 :
105 0 : int NrIceResolverFake::resolve(void *obj,
106 : nr_resolver_resource *resource,
107 : int (*cb)(void *cb_arg,
108 : nr_transport_addr *addr),
109 : void *cb_arg,
110 : void **handle) {
111 : int r,_status;
112 :
113 0 : MOZ_ASSERT(obj);
114 0 : NrIceResolverFake *fake = static_cast<NrIceResolverFake *>(obj);
115 :
116 0 : MOZ_ASSERT(fake->allocated_resolvers_ > 0);
117 :
118 : PendingResolution *pending =
119 : new PendingResolution(fake,
120 : resource->domain_name,
121 0 : resource->port ? resource->port : 3478,
122 0 : resource->transport_protocol ?
123 0 : resource->transport_protocol :
124 : IPPROTO_UDP,
125 0 : resource->address_family,
126 0 : cb, cb_arg);
127 :
128 0 : if ((r=NR_ASYNC_TIMER_SET(fake->delay_ms_,NrIceResolverFake::resolve_cb,
129 : (void *)pending, &pending->timer_handle_))) {
130 0 : delete pending;
131 0 : ABORT(r);
132 : }
133 0 : *handle = pending;
134 :
135 0 : _status=0;
136 : abort:
137 0 : return(_status);
138 : }
139 :
140 0 : void NrIceResolverFake::resolve_cb(NR_SOCKET s, int how, void *cb_arg) {
141 0 : MOZ_ASSERT(cb_arg);
142 0 : PendingResolution *pending = static_cast<PendingResolution *>(cb_arg);
143 :
144 0 : const PRNetAddr *addr=pending->resolver_->Resolve(pending->hostname_,
145 0 : pending->address_family_);
146 :
147 0 : if (addr) {
148 : nr_transport_addr transport_addr;
149 :
150 0 : int r = nr_praddr_to_transport_addr(addr, &transport_addr,
151 0 : pending->transport_, 0);
152 0 : MOZ_ASSERT(!r);
153 0 : if (r)
154 0 : goto abort;
155 :
156 0 : r=nr_transport_addr_set_port(&transport_addr, pending->port_);
157 0 : MOZ_ASSERT(!r);
158 0 : if (r)
159 0 : goto abort;
160 :
161 : /* Fill in the address string */
162 0 : r=nr_transport_addr_fmt_addr_string(&transport_addr);
163 0 : MOZ_ASSERT(!r);
164 0 : if (r)
165 0 : goto abort;
166 :
167 0 : pending->cb_(pending->cb_arg_, &transport_addr);
168 0 : delete pending;
169 0 : return;
170 : }
171 :
172 : abort:
173 : // Resolution failed.
174 0 : pending->cb_(pending->cb_arg_, nullptr);
175 :
176 0 : delete pending;
177 : }
178 :
179 0 : int NrIceResolverFake::cancel(void *obj, void *handle) {
180 0 : MOZ_ASSERT(obj);
181 0 : MOZ_ASSERT(static_cast<NrIceResolverFake *>(obj)->allocated_resolvers_ > 0);
182 :
183 0 : PendingResolution *pending = static_cast<PendingResolution *>(handle);
184 :
185 0 : NR_async_timer_cancel(pending->timer_handle_);
186 0 : delete pending;
187 :
188 0 : return(0);
189 : }
190 :
191 :
192 : } // End of namespace mozilla
|