net_helpers.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright 2008 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_NET_HELPERS_H_
  11. #define RTC_BASE_NET_HELPERS_H_
  12. #if defined(WEBRTC_POSIX)
  13. #include <sys/socket.h>
  14. #elif WEBRTC_WIN
  15. #include <winsock2.h> // NOLINT
  16. #endif
  17. #include <vector>
  18. #include "rtc_base/async_resolver_interface.h"
  19. #include "rtc_base/ip_address.h"
  20. #include "rtc_base/socket_address.h"
  21. #include "rtc_base/synchronization/sequence_checker.h"
  22. #include "rtc_base/system/no_unique_address.h"
  23. #include "rtc_base/system/rtc_export.h"
  24. #include "rtc_base/task_utils/pending_task_safety_flag.h"
  25. #include "rtc_base/thread.h"
  26. #include "rtc_base/thread_annotations.h"
  27. namespace rtc {
  28. // AsyncResolver will perform async DNS resolution, signaling the result on
  29. // the SignalDone from AsyncResolverInterface when the operation completes.
  30. //
  31. // This class is thread-compatible, and all methods and destruction needs to
  32. // happen from the same rtc::Thread, except for Destroy which is allowed to
  33. // happen on another context provided it's not happening concurrently to another
  34. // public API call, and is the last access to the object.
  35. class RTC_EXPORT AsyncResolver : public AsyncResolverInterface {
  36. public:
  37. AsyncResolver();
  38. ~AsyncResolver() override;
  39. void Start(const SocketAddress& addr) override;
  40. bool GetResolvedAddress(int family, SocketAddress* addr) const override;
  41. int GetError() const override;
  42. void Destroy(bool wait) override;
  43. const std::vector<IPAddress>& addresses() const;
  44. private:
  45. void ResolveDone(std::vector<IPAddress> addresses, int error)
  46. RTC_EXCLUSIVE_LOCKS_REQUIRED(sequence_checker_);
  47. void MaybeSelfDestruct();
  48. SocketAddress addr_ RTC_GUARDED_BY(sequence_checker_);
  49. std::vector<IPAddress> addresses_ RTC_GUARDED_BY(sequence_checker_);
  50. int error_ RTC_GUARDED_BY(sequence_checker_);
  51. webrtc::ScopedTaskSafety safety_ RTC_GUARDED_BY(sequence_checker_);
  52. std::unique_ptr<Thread> popup_thread_ RTC_GUARDED_BY(sequence_checker_);
  53. bool recursion_check_ =
  54. false; // Protects against SignalDone calling into Destroy.
  55. bool destroy_called_ = false;
  56. RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker sequence_checker_;
  57. };
  58. // rtc namespaced wrappers for inet_ntop and inet_pton so we can avoid
  59. // the windows-native versions of these.
  60. const char* inet_ntop(int af, const void* src, char* dst, socklen_t size);
  61. int inet_pton(int af, const char* src, void* dst);
  62. bool HasIPv4Enabled();
  63. bool HasIPv6Enabled();
  64. } // namespace rtc
  65. #endif // RTC_BASE_NET_HELPERS_H_