nriceresolverfake.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. // Original author: ekr@rtfm.com
  6. // Some of this code is cut-and-pasted from nICEr. Copyright is:
  7. /*
  8. Copyright (c) 2007, Adobe Systems, Incorporated
  9. All rights reserved.
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are
  12. met:
  13. * Redistributions of source code must retain the above copyright
  14. notice, this list of conditions and the following disclaimer.
  15. * Redistributions in binary form must reproduce the above copyright
  16. notice, this list of conditions and the following disclaimer in the
  17. documentation and/or other materials provided with the distribution.
  18. * Neither the name of Adobe Systems, Network Resonance nor the names of its
  19. contributors may be used to endorse or promote products derived from
  20. this software without specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #ifndef nriceresolverfake_h__
  34. #define nriceresolverfake_h__
  35. #include <map>
  36. #include <string>
  37. #include "nspr.h"
  38. #include "prnetdb.h"
  39. typedef struct nr_resolver_ nr_resolver;
  40. typedef struct nr_resolver_vtbl_ nr_resolver_vtbl;
  41. typedef struct nr_transport_addr_ nr_transport_addr;
  42. typedef struct nr_resolver_resource_ nr_resolver_resource;
  43. namespace mozilla {
  44. class NrIceResolverFake {
  45. public:
  46. NrIceResolverFake();
  47. ~NrIceResolverFake();
  48. void SetAddr(const std::string& hostname, const PRNetAddr& addr) {
  49. switch (addr.raw.family) {
  50. case AF_INET:
  51. addrs_[hostname] = addr;
  52. break;
  53. case AF_INET6:
  54. addrs6_[hostname] = addr;
  55. break;
  56. default:
  57. MOZ_CRASH();
  58. }
  59. }
  60. nr_resolver *AllocateResolver();
  61. void DestroyResolver();
  62. private:
  63. // Implementations of vtbl functions
  64. static int destroy(void **objp);
  65. static int resolve(void *obj,
  66. nr_resolver_resource *resource,
  67. int (*cb)(void *cb_arg,
  68. nr_transport_addr *addr),
  69. void *cb_arg,
  70. void **handle);
  71. static void resolve_cb(NR_SOCKET s, int how, void *cb_arg);
  72. static int cancel(void *obj, void *handle);
  73. // Get an address.
  74. const PRNetAddr *Resolve(const std::string& hostname, int address_family) {
  75. switch (address_family) {
  76. case AF_INET:
  77. if (!addrs_.count(hostname))
  78. return nullptr;
  79. return &addrs_[hostname];
  80. case AF_INET6:
  81. if (!addrs6_.count(hostname))
  82. return nullptr;
  83. return &addrs6_[hostname];
  84. default:
  85. MOZ_CRASH();
  86. }
  87. }
  88. struct PendingResolution {
  89. PendingResolution(NrIceResolverFake *resolver,
  90. const std::string& hostname,
  91. uint16_t port,
  92. int transport,
  93. int address_family,
  94. int (*cb)(void *cb_arg, nr_transport_addr *addr),
  95. void *cb_arg) :
  96. resolver_(resolver),
  97. hostname_(hostname),
  98. port_(port),
  99. transport_(transport),
  100. address_family_(address_family),
  101. cb_(cb), cb_arg_(cb_arg) {}
  102. NrIceResolverFake *resolver_;
  103. std::string hostname_;
  104. uint16_t port_;
  105. int transport_;
  106. int address_family_;
  107. int (*cb_)(void *cb_arg, nr_transport_addr *addr);
  108. void *cb_arg_;
  109. void *timer_handle_;
  110. };
  111. nr_resolver_vtbl* vtbl_;
  112. std::map<std::string, PRNetAddr> addrs_;
  113. std::map<std::string, PRNetAddr> addrs6_;
  114. uint32_t delay_ms_;
  115. int allocated_resolvers_;
  116. };
  117. } // End of namespace mozilla
  118. #endif