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/. */
8 // Original author: ekr@rtfm.com
10 // Some of this code is cut-and-pasted from nICEr. Copyright is:
13 Copyright (c) 2007, Adobe Systems, Incorporated
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions are
20 * Redistributions of source code must retain the above copyright
21 notice, this list of conditions and the following disclaimer.
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.
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.
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.
47 #include "mozilla/Assertions.h"
51 #include "async_timer.h"
52 #include "nr_resolver.h"
53 #include "transport_addr.h"
56 #include "nriceresolverfake.h"
57 #include "nr_socket_prsock.h"
61 NrIceResolverFake::NrIceResolverFake() :
62 vtbl_(new nr_resolver_vtbl
), addrs_(), delay_ms_(100),
63 allocated_resolvers_(0) {
64 vtbl_
->destroy
= &NrIceResolverFake::destroy
;
65 vtbl_
->resolve
= &NrIceResolverFake::resolve
;
66 vtbl_
->cancel
= &NrIceResolverFake::cancel
;
69 NrIceResolverFake::~NrIceResolverFake() {
70 MOZ_ASSERT(allocated_resolvers_
== 0);
75 nr_resolver
*NrIceResolverFake::AllocateResolver() {
76 nr_resolver
*resolver
;
78 int r
= nr_resolver_create_int((void *)this,
84 ++allocated_resolvers_
;
89 void NrIceResolverFake::DestroyResolver() {
90 --allocated_resolvers_
;
93 int NrIceResolverFake::destroy(void **objp
) {
97 NrIceResolverFake
*fake
= static_cast<NrIceResolverFake
*>(*objp
);
100 fake
->DestroyResolver();
105 int NrIceResolverFake::resolve(void *obj
,
106 nr_resolver_resource
*resource
,
107 int (*cb
)(void *cb_arg
,
108 nr_transport_addr
*addr
),
114 NrIceResolverFake
*fake
= static_cast<NrIceResolverFake
*>(obj
);
116 MOZ_ASSERT(fake
->allocated_resolvers_
> 0);
118 PendingResolution
*pending
=
119 new PendingResolution(fake
,
120 resource
->domain_name
,
121 resource
->port
? resource
->port
: 3478,
122 resource
->transport_protocol
?
123 resource
->transport_protocol
:
127 if ((r
=NR_ASYNC_TIMER_SET(fake
->delay_ms_
,NrIceResolverFake::resolve_cb
,
128 (void *)pending
, &pending
->timer_handle_
))) {
139 void NrIceResolverFake::resolve_cb(NR_SOCKET s
, int how
, void *cb_arg
) {
141 PendingResolution
*pending
= static_cast<PendingResolution
*>(cb_arg
);
143 const PRNetAddr
*addr
=pending
->resolver_
->Resolve(pending
->hostname_
);
146 nr_transport_addr transport_addr
;
148 int r
= nr_praddr_to_transport_addr(addr
, &transport_addr
,
149 pending
->transport_
, 0);
154 r
=nr_transport_addr_set_port(&transport_addr
, pending
->port_
);
159 /* Fill in the address string */
160 r
=nr_transport_addr_fmt_addr_string(&transport_addr
);
165 pending
->cb_(pending
->cb_arg_
, &transport_addr
);
171 // Resolution failed.
172 pending
->cb_(pending
->cb_arg_
, nullptr);
177 int NrIceResolverFake::cancel(void *obj
, void *handle
) {
179 MOZ_ASSERT(static_cast<NrIceResolverFake
*>(obj
)->allocated_resolvers_
> 0);
181 PendingResolution
*pending
= static_cast<PendingResolution
*>(handle
);
183 NR_async_timer_cancel(pending
->timer_handle_
);
190 } // End of namespace mozilla