fake_network.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 2009 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_FAKE_NETWORK_H_
  11. #define RTC_BASE_FAKE_NETWORK_H_
  12. #include <memory>
  13. #include <string>
  14. #include <utility>
  15. #include <vector>
  16. #include "absl/memory/memory.h"
  17. #include "rtc_base/checks.h"
  18. #include "rtc_base/mdns_responder_interface.h"
  19. #include "rtc_base/message_handler.h"
  20. #include "rtc_base/network.h"
  21. #include "rtc_base/socket_address.h"
  22. #include "rtc_base/string_encode.h"
  23. #include "rtc_base/thread.h"
  24. namespace rtc {
  25. const int kFakeIPv4NetworkPrefixLength = 24;
  26. const int kFakeIPv6NetworkPrefixLength = 64;
  27. // Fake network manager that allows us to manually specify the IPs to use.
  28. class FakeNetworkManager : public NetworkManagerBase,
  29. public MessageHandlerAutoCleanup {
  30. public:
  31. FakeNetworkManager() {}
  32. typedef std::vector<std::pair<SocketAddress, AdapterType>> IfaceList;
  33. void AddInterface(const SocketAddress& iface) {
  34. // Ensure a unique name for the interface if its name is not given.
  35. AddInterface(iface, "test" + rtc::ToString(next_index_++));
  36. }
  37. void AddInterface(const SocketAddress& iface, const std::string& if_name) {
  38. AddInterface(iface, if_name, ADAPTER_TYPE_UNKNOWN);
  39. }
  40. void AddInterface(const SocketAddress& iface,
  41. const std::string& if_name,
  42. AdapterType type) {
  43. SocketAddress address(if_name, 0);
  44. address.SetResolvedIP(iface.ipaddr());
  45. ifaces_.push_back(std::make_pair(address, type));
  46. DoUpdateNetworks();
  47. }
  48. void RemoveInterface(const SocketAddress& iface) {
  49. for (IfaceList::iterator it = ifaces_.begin(); it != ifaces_.end(); ++it) {
  50. if (it->first.EqualIPs(iface)) {
  51. ifaces_.erase(it);
  52. break;
  53. }
  54. }
  55. DoUpdateNetworks();
  56. }
  57. void StartUpdating() override {
  58. ++start_count_;
  59. if (start_count_ == 1) {
  60. sent_first_update_ = false;
  61. rtc::Thread::Current()->Post(RTC_FROM_HERE, this);
  62. } else {
  63. if (sent_first_update_) {
  64. SignalNetworksChanged();
  65. }
  66. }
  67. }
  68. void StopUpdating() override { --start_count_; }
  69. // MessageHandler interface.
  70. void OnMessage(Message* msg) override { DoUpdateNetworks(); }
  71. using NetworkManagerBase::set_default_local_addresses;
  72. using NetworkManagerBase::set_enumeration_permission;
  73. // rtc::NetworkManager override.
  74. webrtc::MdnsResponderInterface* GetMdnsResponder() const override {
  75. return mdns_responder_.get();
  76. }
  77. void set_mdns_responder(
  78. std::unique_ptr<webrtc::MdnsResponderInterface> mdns_responder) {
  79. mdns_responder_ = std::move(mdns_responder);
  80. }
  81. private:
  82. void DoUpdateNetworks() {
  83. if (start_count_ == 0)
  84. return;
  85. std::vector<Network*> networks;
  86. for (IfaceList::iterator it = ifaces_.begin(); it != ifaces_.end(); ++it) {
  87. int prefix_length = 0;
  88. if (it->first.ipaddr().family() == AF_INET) {
  89. prefix_length = kFakeIPv4NetworkPrefixLength;
  90. } else if (it->first.ipaddr().family() == AF_INET6) {
  91. prefix_length = kFakeIPv6NetworkPrefixLength;
  92. }
  93. IPAddress prefix = TruncateIP(it->first.ipaddr(), prefix_length);
  94. std::unique_ptr<Network> net(new Network(it->first.hostname(),
  95. it->first.hostname(), prefix,
  96. prefix_length, it->second));
  97. net->set_default_local_address_provider(this);
  98. net->AddIP(it->first.ipaddr());
  99. networks.push_back(net.release());
  100. }
  101. bool changed;
  102. MergeNetworkList(networks, &changed);
  103. if (changed || !sent_first_update_) {
  104. SignalNetworksChanged();
  105. sent_first_update_ = true;
  106. }
  107. }
  108. IfaceList ifaces_;
  109. int next_index_ = 0;
  110. int start_count_ = 0;
  111. bool sent_first_update_ = false;
  112. std::unique_ptr<webrtc::MdnsResponderInterface> mdns_responder_;
  113. };
  114. } // namespace rtc
  115. #endif // RTC_BASE_FAKE_NETWORK_H_