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