nriceresolver.h 4.0 KB

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