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