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 : #ifndef nriceresolverfake_h__
45 : #define nriceresolverfake_h__
46 :
47 : #include <map>
48 : #include <string>
49 :
50 : #include "nspr.h"
51 : #include "prnetdb.h"
52 :
53 : typedef struct nr_resolver_ nr_resolver;
54 : typedef struct nr_resolver_vtbl_ nr_resolver_vtbl;
55 : typedef struct nr_transport_addr_ nr_transport_addr;
56 : typedef struct nr_resolver_resource_ nr_resolver_resource;
57 :
58 : namespace mozilla {
59 :
60 : class NrIceResolverFake {
61 : public:
62 : NrIceResolverFake();
63 : ~NrIceResolverFake();
64 :
65 : void SetAddr(const std::string& hostname, const PRNetAddr& addr) {
66 : switch (addr.raw.family) {
67 : case AF_INET:
68 : addrs_[hostname] = addr;
69 : break;
70 : case AF_INET6:
71 : addrs6_[hostname] = addr;
72 : break;
73 : default:
74 : MOZ_CRASH();
75 : }
76 : }
77 :
78 : nr_resolver *AllocateResolver();
79 :
80 : void DestroyResolver();
81 :
82 : private:
83 : // Implementations of vtbl functions
84 : static int destroy(void **objp);
85 : static int resolve(void *obj,
86 : nr_resolver_resource *resource,
87 : int (*cb)(void *cb_arg,
88 : nr_transport_addr *addr),
89 : void *cb_arg,
90 : void **handle);
91 : static void resolve_cb(NR_SOCKET s, int how, void *cb_arg);
92 : static int cancel(void *obj, void *handle);
93 :
94 : // Get an address.
95 0 : const PRNetAddr *Resolve(const std::string& hostname, int address_family) {
96 0 : switch (address_family) {
97 : case AF_INET:
98 0 : if (!addrs_.count(hostname))
99 0 : return nullptr;
100 :
101 0 : return &addrs_[hostname];
102 : case AF_INET6:
103 0 : if (!addrs6_.count(hostname))
104 0 : return nullptr;
105 :
106 0 : return &addrs6_[hostname];
107 : default:
108 0 : MOZ_CRASH();
109 : }
110 : }
111 :
112 :
113 0 : struct PendingResolution {
114 0 : PendingResolution(NrIceResolverFake *resolver,
115 : const std::string& hostname,
116 : uint16_t port,
117 : int transport,
118 : int address_family,
119 : int (*cb)(void *cb_arg, nr_transport_addr *addr),
120 0 : void *cb_arg) :
121 : resolver_(resolver),
122 : hostname_(hostname),
123 : port_(port),
124 : transport_(transport),
125 : address_family_(address_family),
126 0 : cb_(cb), cb_arg_(cb_arg) {}
127 :
128 : NrIceResolverFake *resolver_;
129 : std::string hostname_;
130 : uint16_t port_;
131 : int transport_;
132 : int address_family_;
133 : int (*cb_)(void *cb_arg, nr_transport_addr *addr);
134 : void *cb_arg_;
135 : void *timer_handle_;
136 : };
137 :
138 : nr_resolver_vtbl* vtbl_;
139 : std::map<std::string, PRNetAddr> addrs_;
140 : std::map<std::string, PRNetAddr> addrs6_;
141 : uint32_t delay_ms_;
142 : int allocated_resolvers_;
143 : };
144 :
145 : } // End of namespace mozilla
146 :
147 : #endif
|