You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
4.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2009 Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_FAKENETWORK_H_
  28. #define TALK_BASE_FAKENETWORK_H_
  29. #include <string>
  30. #include <vector>
  31. #include "talk/base/network.h"
  32. #include "talk/base/messagehandler.h"
  33. #include "talk/base/socketaddress.h"
  34. #include "talk/base/stringencode.h"
  35. #include "talk/base/thread.h"
  36. namespace talk_base {
  37. const int kFakeIPv4NetworkPrefixLength = 24;
  38. const int kFakeIPv6NetworkPrefixLength = 64;
  39. // Fake network manager that allows us to manually specify the IPs to use.
  40. class FakeNetworkManager : public NetworkManagerBase,
  41. public MessageHandler {
  42. public:
  43. FakeNetworkManager()
  44. : thread_(Thread::Current()),
  45. next_index_(0),
  46. started_(false),
  47. sent_first_update_(false) {
  48. }
  49. typedef std::vector<SocketAddress> IfaceList;
  50. void AddInterface(const SocketAddress& iface) {
  51. // ensure a unique name for the interface
  52. SocketAddress address("test" + talk_base::ToString(next_index_++), 0);
  53. address.SetResolvedIP(iface.ipaddr());
  54. ifaces_.push_back(address);
  55. DoUpdateNetworks();
  56. }
  57. void RemoveInterface(const SocketAddress& iface) {
  58. for (IfaceList::iterator it = ifaces_.begin();
  59. it != ifaces_.end(); ++it) {
  60. if (it->EqualIPs(iface)) {
  61. ifaces_.erase(it);
  62. break;
  63. }
  64. }
  65. DoUpdateNetworks();
  66. }
  67. virtual void StartUpdating() {
  68. if (started_) {
  69. if (sent_first_update_)
  70. SignalNetworksChanged();
  71. return;
  72. }
  73. started_ = true;
  74. sent_first_update_ = false;
  75. thread_->Post(this);
  76. }
  77. virtual void StopUpdating() {
  78. started_ = false;
  79. }
  80. // MessageHandler interface.
  81. virtual void OnMessage(Message* msg) {
  82. DoUpdateNetworks();
  83. }
  84. private:
  85. void DoUpdateNetworks() {
  86. if (!started_)
  87. return;
  88. std::vector<Network*> networks;
  89. for (IfaceList::iterator it = ifaces_.begin();
  90. it != ifaces_.end(); ++it) {
  91. int prefix_length = 0;
  92. if (it->ipaddr().family() == AF_INET) {
  93. prefix_length = kFakeIPv4NetworkPrefixLength;
  94. } else if (it->ipaddr().family() == AF_INET6) {
  95. prefix_length = kFakeIPv6NetworkPrefixLength;
  96. }
  97. IPAddress prefix = TruncateIP(it->ipaddr(), prefix_length);
  98. scoped_ptr<Network> net(new Network(it->hostname(),
  99. it->hostname(),
  100. prefix,
  101. prefix_length));
  102. net->AddIP(it->ipaddr());
  103. networks.push_back(net.release());
  104. }
  105. bool changed;
  106. MergeNetworkList(networks, &changed);
  107. if (changed || !sent_first_update_) {
  108. SignalNetworksChanged();
  109. sent_first_update_ = true;
  110. }
  111. }
  112. Thread* thread_;
  113. IfaceList ifaces_;
  114. int next_index_;
  115. bool started_;
  116. bool sent_first_update_;
  117. };
  118. } // namespace talk_base
  119. #endif // TALK_BASE_FAKENETWORK_H_