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 authors: jib@mozilla.com, 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 #ifndef nriceresolver_h__
44 #define nriceresolver_h__
45 
46 #include <map>
47 #include <string>
48 #include "nspr.h"
49 #include "prnetdb.h"
50 #include "nsIDNSService.h"
51 #include "nsIDNSListener.h"
52 #include "nsICancelable.h"
53 #include "nricectx.h"
54 
55 typedef struct nr_resolver_ nr_resolver;
56 typedef struct nr_resolver_vtbl_ nr_resolver_vtbl;
57 typedef struct nr_transport_addr_ nr_transport_addr;
58 typedef struct nr_resolver_resource_ nr_resolver_resource;
59 
60 namespace mozilla {
61 
62 class NrIceResolver {
63  private:
64   ~NrIceResolver();
65 
66  public:
67   NrIceResolver();
68 
69   nsresult Init();
70   nr_resolver* AllocateResolver();
71   void DestroyResolver();
72   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(NrIceResolver)
73 
74   int resolve(nr_resolver_resource* resource,
75               int (*cb)(void* cb_arg, nr_transport_addr* addr), void* cb_arg,
76               void** handle);
77 
78  private:
79   // Implementations of vtbl functions
80   static int destroy(void** objp);
81   static int resolve(void* obj, nr_resolver_resource* resource,
82                      int (*cb)(void* cb_arg, nr_transport_addr* addr),
83                      void* cb_arg, void** handle);
84   static void resolve_cb(NR_SOCKET s, int how, void* cb_arg);
85   static int cancel(void* obj, void* handle);
86 
87   class PendingResolution : public nsIDNSListener {
88    public:
PendingResolution(nsIEventTarget * thread,uint16_t port,int transport,int (* cb)(void * cb_arg,nr_transport_addr * addr),void * cb_arg)89     PendingResolution(nsIEventTarget* thread, uint16_t port, int transport,
90                       int (*cb)(void* cb_arg, nr_transport_addr* addr),
91                       void* cb_arg)
92         : thread_(thread),
93           port_(port),
94           transport_(transport),
95           cb_(cb),
96           cb_arg_(cb_arg) {}
97     NS_IMETHOD OnLookupComplete(nsICancelable* request, nsIDNSRecord* record,
98                                 nsresult status) override;
99 
100     int cancel();
101     nsCOMPtr<nsICancelable> request_;
102     NS_DECL_THREADSAFE_ISUPPORTS
103 
104    private:
105     virtual ~PendingResolution() = default;
106 
107     nsCOMPtr<nsIEventTarget> thread_;
108     uint16_t port_;
109     int transport_;
110     int (*cb_)(void* cb_arg, nr_transport_addr* addr);
111     void* cb_arg_;
112   };
113 
114   nr_resolver_vtbl* vtbl_;
115   nsCOMPtr<nsIEventTarget> sts_thread_;
116   nsCOMPtr<nsIDNSService> dns_;
117 #ifdef DEBUG
118   int allocated_resolvers_;
119 #endif
120 };
121 
122 }  // End of namespace mozilla
123 #endif
124