net_helpers.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/rtc_export.h"
  23. #include "rtc_base/task_utils/pending_task_safety_flag.h"
  24. #include "rtc_base/thread.h"
  25. #include "rtc_base/thread_annotations.h"
  26. namespace rtc {
  27. // AsyncResolver will perform async DNS resolution, signaling the result on
  28. // the SignalDone from AsyncResolverInterface when the operation completes.
  29. //
  30. // This class is thread-compatible, and all methods and destruction needs to
  31. // happen from the same rtc::Thread, except for Destroy which is allowed to
  32. // happen on another context provided it's not happening concurrently to another
  33. // public API call, and is the last access to the object.
  34. class RTC_EXPORT AsyncResolver : public AsyncResolverInterface {
  35. public:
  36. AsyncResolver();
  37. ~AsyncResolver() override;
  38. void Start(const SocketAddress& addr) override;
  39. bool GetResolvedAddress(int family, SocketAddress* addr) const override;
  40. int GetError() const override;
  41. void Destroy(bool wait) override;
  42. const std::vector<IPAddress>& addresses() const;
  43. private:
  44. void ResolveDone(std::vector<IPAddress> addresses, int error)
  45. RTC_EXCLUSIVE_LOCKS_REQUIRED(sequence_checker_);
  46. void MaybeSelfDestruct();
  47. SocketAddress addr_ RTC_GUARDED_BY(sequence_checker_);
  48. std::vector<IPAddress> addresses_ RTC_GUARDED_BY(sequence_checker_);
  49. int error_ RTC_GUARDED_BY(sequence_checker_);
  50. webrtc::ScopedTaskSafety safety_ RTC_GUARDED_BY(sequence_checker_);
  51. std::unique_ptr<Thread> popup_thread_ RTC_GUARDED_BY(sequence_checker_);
  52. bool recursion_check_ =
  53. false; // Protects against SignalDone calling into Destroy.
  54. bool destroy_called_ = false;
  55. webrtc::SequenceChecker sequence_checker_;
  56. };
  57. // rtc namespaced wrappers for inet_ntop and inet_pton so we can avoid
  58. // the windows-native versions of these.
  59. const char* inet_ntop(int af, const void* src, char* dst, socklen_t size);
  60. int inet_pton(int af, const char* src, void* dst);
  61. bool HasIPv4Enabled();
  62. bool HasIPv6Enabled();
  63. } // namespace rtc
  64. #endif // RTC_BASE_NET_HELPERS_H_