Implemented Network::GetBestIP() selection logic as following.

1) return the first global temporary and non-deprecrated ones.
2) if #1 not available, return global one.
3) if #2 not available, use ULA ipv6 as last resort.

ULA stands for unique local address. They are only useful in a private
WebRTC deployment. More detail: http://en.wikipedia.org/wiki/Unique_local_address

BUG=3808

At this point, rule #3 actually won't happen at current
implementation. The reason being that ULA address starting with 0xfc 0r 0xfd will be grouped into its own Network. The result of that is WebRTC will have one extra Network to generate candidates but the lack of rule #3 shouldn't prevent turning on IPv6 since ULA should only be tried in a close deployment anyway.

R=jiayl@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/31369004

git-svn-id: http://webrtc.googlecode.com/svn/trunk/talk@7200 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/p2p/client/basicportallocator.cc b/p2p/client/basicportallocator.cc
index eda0b4b..9ca873b 100644
--- a/p2p/client/basicportallocator.cc
+++ b/p2p/client/basicportallocator.cc
@@ -431,7 +431,7 @@
       }
 
       if (!(sequence_flags & PORTALLOCATOR_ENABLE_IPV6) &&
-          networks[i]->ip().family() == AF_INET6) {
+          networks[i]->GetBestIP().family() == AF_INET6) {
         // Skip IPv6 networks unless the flag's been set.
         continue;
       }
@@ -728,7 +728,7 @@
                                        uint32 flags)
     : session_(session),
       network_(network),
-      ip_(network->ip()),
+      ip_(network->GetBestIP()),
       config_(config),
       state_(kInit),
       flags_(flags),
@@ -771,7 +771,7 @@
 
 void AllocationSequence::DisableEquivalentPhases(rtc::Network* network,
     PortConfiguration* config, uint32* flags) {
-  if (!((network == network_) && (ip_ == network->ip()))) {
+  if (!((network == network_) && (ip_ == network->GetBestIP()))) {
     // Different network setup; nothing is equivalent.
     return;
   }
diff --git a/p2p/client/connectivitychecker.cc b/p2p/client/connectivitychecker.cc
index 06de5e4..6e277a4 100644
--- a/p2p/client/connectivitychecker.cc
+++ b/p2p/client/connectivitychecker.cc
@@ -1,5 +1,29 @@
-// Copyright 2011 Google Inc. All Rights Reserved.
-
+/*
+ * libjingle
+ * Copyright 2004--2005, Google Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *  1. Redistributions of source code must retain the above copyright notice,
+ *     this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright notice,
+ *     this list of conditions and the following disclaimer in the documentation
+ *     and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *     derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+ * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 
 #include <string>
 
@@ -214,7 +238,8 @@
     return;
   }
   rtc::ProxyInfo proxy_info = request->proxy();
-  NicMap::iterator i = nics_.find(NicId(networks[0]->ip(), proxy_info.address));
+  NicMap::iterator i =
+      nics_.find(NicId(networks[0]->GetBestIP(), proxy_info.address));
   if (i != nics_.end()) {
     int port = request->port();
     uint32 now = rtc::Time();
@@ -247,7 +272,7 @@
   ASSERT(worker_ == rtc::Thread::Current());
   RelayPort* relay_port = reinterpret_cast<RelayPort*>(port);
   const ProtocolAddress* address = relay_port->ServerAddress(0);
-  rtc::IPAddress ip = port->Network()->ip();
+  rtc::IPAddress ip = port->Network()->GetBestIP();
   NicMap::iterator i = nics_.find(NicId(ip, port->proxy().address));
   if (i != nics_.end()) {
     // We have it already, add the new information.
@@ -281,7 +306,7 @@
   ASSERT(worker_ == rtc::Thread::Current());
   const std::vector<Candidate> candidates = port->Candidates();
   Candidate c = candidates[0];
-  rtc::IPAddress ip = port->Network()->ip();
+  rtc::IPAddress ip = port->Network()->GetBestIP();
   NicMap::iterator i = nics_.find(NicId(ip, port->proxy().address));
   if (i != nics_.end()) {
     // We have it already, add the new information.
@@ -300,7 +325,7 @@
 void ConnectivityChecker::OnStunPortError(Port* port) {
   ASSERT(worker_ == rtc::Thread::Current());
   LOG(LS_ERROR) << "Stun address error.";
-  rtc::IPAddress ip = port->Network()->ip();
+  rtc::IPAddress ip = port->Network()->GetBestIP();
   NicMap::iterator i = nics_.find(NicId(ip, port->proxy().address));
   if (i != nics_.end()) {
     // We have it already, add the new information.
@@ -337,19 +362,28 @@
 StunPort* ConnectivityChecker::CreateStunPort(
     const std::string& username, const std::string& password,
     const PortConfiguration* config, rtc::Network* network) {
-  return StunPort::Create(worker_, socket_factory_.get(),
-                          network, network->ip(), 0, 0,
-                          username, password, config->stun_servers);
+  return StunPort::Create(worker_,
+                          socket_factory_.get(),
+                          network,
+                          network->GetBestIP(),
+                          0,
+                          0,
+                          username,
+                          password,
+                          config->stun_servers);
 }
 
 RelayPort* ConnectivityChecker::CreateRelayPort(
     const std::string& username, const std::string& password,
     const PortConfiguration* config, rtc::Network* network) {
-  return RelayPort::Create(worker_, socket_factory_.get(),
-                           network, network->ip(),
+  return RelayPort::Create(worker_,
+                           socket_factory_.get(),
+                           network,
+                           network->GetBestIP(),
                            port_allocator_->min_port(),
                            port_allocator_->max_port(),
-                           username, password);
+                           username,
+                           password);
 }
 
 void ConnectivityChecker::CreateRelayPorts(
@@ -365,8 +399,8 @@
   for (relay = config->relays.begin();
        relay != config->relays.end(); ++relay) {
     for (uint32 i = 0; i < networks.size(); ++i) {
-      NicMap::iterator iter = nics_.find(NicId(networks[i]->ip(),
-                                               proxy_info.address));
+      NicMap::iterator iter =
+          nics_.find(NicId(networks[i]->GetBestIP(), proxy_info.address));
       if (iter != nics_.end()) {
         // TODO: Now setting the same start time for all protocols.
         // This might affect accuracy, but since we are mainly looking for
@@ -423,7 +457,7 @@
   rtc::ProxyInfo proxy_info = GetProxyInfo();
   bool allocate_relay_ports = false;
   for (uint32 i = 0; i < networks.size(); ++i) {
-    if (AddNic(networks[i]->ip(), proxy_info.address)) {
+    if (AddNic(networks[i]->GetBestIP(), proxy_info.address)) {
       Port* port = CreateStunPort(username, password, &config, networks[i]);
       if (port) {
 
@@ -500,7 +534,8 @@
     return;
   }
   rtc::ProxyInfo proxy_info = GetProxyInfo();
-  NicMap::iterator i = nics_.find(NicId(networks[0]->ip(), proxy_info.address));
+  NicMap::iterator i =
+      nics_.find(NicId(networks[0]->GetBestIP(), proxy_info.address));
   if (i != nics_.end()) {
     uint32 now = rtc::Time();
     NicInfo* nic_info = &i->second;
diff --git a/p2p/client/connectivitychecker_unittest.cc b/p2p/client/connectivitychecker_unittest.cc
index b96cf17..e206ad7 100644
--- a/p2p/client/connectivitychecker_unittest.cc
+++ b/p2p/client/connectivitychecker_unittest.cc
@@ -1,5 +1,29 @@
-// Copyright 2011 Google Inc. All Rights Reserved.
-
+/*
+ * libjingle
+ * Copyright 2004--2005, Google Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *  1. Redistributions of source code must retain the above copyright notice,
+ *     this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright notice,
+ *     this list of conditions and the following disclaimer in the documentation
+ *     and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *     derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+ * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 
 #include <string>
 
@@ -211,19 +235,27 @@
   virtual StunPort* CreateStunPort(
       const std::string& username, const std::string& password,
       const PortConfiguration* config, rtc::Network* network) {
-    return new FakeStunPort(worker(), socket_factory_,
-                            network, network->ip(),
-                            kMinPort, kMaxPort,
-                            username, password,
+    return new FakeStunPort(worker(),
+                            socket_factory_,
+                            network,
+                            network->GetBestIP(),
+                            kMinPort,
+                            kMaxPort,
+                            username,
+                            password,
                             config->stun_servers);
   }
   virtual RelayPort* CreateRelayPort(
       const std::string& username, const std::string& password,
       const PortConfiguration* config, rtc::Network* network) {
-    return new FakeRelayPort(worker(), socket_factory_,
-                             network, network->ip(),
-                             kMinPort, kMaxPort,
-                             username, password);
+    return new FakeRelayPort(worker(),
+                             socket_factory_,
+                             network,
+                             network->GetBestIP(),
+                             kMinPort,
+                             kMaxPort,
+                             username,
+                             password);
   }
   virtual void InitiateProxyDetection() {
     if (!proxy_initiated_) {
diff --git a/p2p/client/fakeportallocator.h b/p2p/client/fakeportallocator.h
index 6c36c4e..f80f0bd 100644
--- a/p2p/client/fakeportallocator.h
+++ b/p2p/client/fakeportallocator.h
@@ -1,6 +1,29 @@
-// Copyright 2010 Google Inc. All Rights Reserved,
-//
-// Author: Justin Uberti (juberti@google.com)
+/*
+ * libjingle
+ * Copyright 2004--2005, Google Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *  1. Redistributions of source code must retain the above copyright notice,
+ *     this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright notice,
+ *     this list of conditions and the following disclaimer in the documentation
+ *     and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *     derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+ * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 
 #ifndef TALK_P2P_CLIENT_FAKEPORTALLOCATOR_H_
 #define TALK_P2P_CLIENT_FAKEPORTALLOCATOR_H_
@@ -39,10 +62,14 @@
 
   virtual void StartGettingPorts() {
     if (!port_) {
-      port_.reset(cricket::UDPPort::Create(worker_thread_, factory_,
-                      &network_, network_.ip(), 0, 0,
-                      username(),
-                      password()));
+      port_.reset(cricket::UDPPort::Create(worker_thread_,
+                                           factory_,
+                                           &network_,
+                                           network_.GetBestIP(),
+                                           0,
+                                           0,
+                                           username(),
+                                           password()));
       AddPort(port_.get());
     }
     ++port_config_count_;