Mark TCP connections that fail initialization as failed.

This silences some spurious messages that were generated
by https://chromium-review.googlesource.com/c/chromium/src/+/1986070

Bug: chromium:1038754
Change-Id: I950b82c01a7e5be1f5e910b148c0b201f814f430
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/164529
Reviewed-by: Qingsi Wang <qingsi@webrtc.org>
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#30156}
diff --git a/p2p/base/p2p_transport_channel.cc b/p2p/base/p2p_transport_channel.cc
index 781f709..c7cfe5a 100644
--- a/p2p/base/p2p_transport_channel.cc
+++ b/p2p/base/p2p_transport_channel.cc
@@ -1773,7 +1773,8 @@
         // TODO(deadbeef): Once we implement end-of-candidates signaling,
         // we shouldn't go from INIT to COMPLETED.
         RTC_DCHECK(state == IceTransportState::STATE_CONNECTING ||
-                   state == IceTransportState::STATE_COMPLETED);
+                   state == IceTransportState::STATE_COMPLETED ||
+                   state == IceTransportState::STATE_FAILED);
         break;
       case IceTransportState::STATE_CONNECTING:
         RTC_DCHECK(state == IceTransportState::STATE_COMPLETED ||
diff --git a/p2p/base/tcp_port.cc b/p2p/base/tcp_port.cc
index 6e5b8dc..d1fb9b2 100644
--- a/p2p/base/tcp_port.cc
+++ b/p2p/base/tcp_port.cc
@@ -211,14 +211,24 @@
       return SOCKET_ERROR;
     }
     socket = conn->socket();
+    if (!socket) {
+      // The failure to initialize should have been logged elsewhere,
+      // so this log is not important.
+      RTC_LOG(LS_INFO) << ToString()
+                       << ": Attempted to send to an uninitialized socket: "
+                       << addr.ToSensitiveString();
+      error_ = EHOSTUNREACH;
+      return SOCKET_ERROR;
+    }
   } else {
     socket = GetIncoming(addr);
-  }
-  if (!socket) {
-    RTC_LOG(LS_ERROR) << ToString()
-                      << ": Attempted to send to an unknown destination: "
-                      << addr.ToSensitiveString();
-    return SOCKET_ERROR;  // TODO(tbd): Set error_
+    if (!socket) {
+      RTC_LOG(LS_ERROR) << ToString()
+                        << ": Attempted to send to an unknown destination: "
+                        << addr.ToSensitiveString();
+      error_ = EHOSTUNREACH;
+      return SOCKET_ERROR;
+    }
   }
   rtc::PacketOptions modified_options(options);
   CopyPortInformationToPacketInfo(&modified_options.info_signaled_after_sent);
@@ -546,7 +556,6 @@
 
 void TCPConnection::CreateOutgoingTcpSocket() {
   RTC_DCHECK(outgoing_);
-  // TODO(guoweis): Handle failures here (unlikely since TCP).
   int opts = (remote_candidate().protocol() == SSLTCP_PROTOCOL_NAME)
                  ? rtc::PacketSocketFactory::OPT_TLS_FAKE
                  : 0;
@@ -567,6 +576,7 @@
   } else {
     RTC_LOG(LS_WARNING) << ToString() << ": Failed to create connection to "
                         << remote_candidate().address().ToSensitiveString();
+    FailAndPrune();
   }
 }