Refer to the underlying object when reporting the state of SSL basic I/O

The reasons behind this change:

1. In OpenSSL 1.1.0. BIO will be an opaque object. We won't have direct access to the `num` field.
2. `num` is only used by OpenSSL provided BIOs and different types of BIOs use num differently.
WebRTC is providing its own customized BIO implementation, it probably shouldn't piggyback into
this internal field to store the stream/socket state.
4. We can access the stream/socket state directly using the underlying object anyway.


Bug: webrtc:8817
Change-Id: I41cdd2920fba378e312e8436a7b9733381555522
Reviewed-on: https://webrtc-review.googlesource.com/46360
Commit-Queue: Jiawei Ou <ouj@fb.com>
Reviewed-by: Tommi <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#21814}
diff --git a/rtc_base/openssladapter.cc b/rtc_base/openssladapter.cc
index a064596..e3643a5 100644
--- a/rtc_base/openssladapter.cc
+++ b/rtc_base/openssladapter.cc
@@ -94,7 +94,6 @@
 static int socket_new(BIO* b) {
   b->shutdown = 0;
   b->init = 1;
-  b->num = 0; // 1 means socket closed
   b->ptr = 0;
   return 1;
 }
@@ -113,8 +112,6 @@
   int result = socket->Recv(out, outl, nullptr);
   if (result > 0) {
     return result;
-  } else if (result == 0) {
-    b->num = 1;
   } else if (socket->IsBlocking()) {
     BIO_set_retry_read(b);
   }
@@ -143,8 +140,11 @@
   switch (cmd) {
   case BIO_CTRL_RESET:
     return 0;
-  case BIO_CTRL_EOF:
-    return b->num;
+  case BIO_CTRL_EOF: {
+    rtc::AsyncSocket* socket = static_cast<rtc::AsyncSocket*>(ptr);
+    // 1 means socket closed.
+    return (socket->GetState() == rtc::AsyncSocket::CS_CLOSED) ? 1 : 0;
+  }
   case BIO_CTRL_WPENDING:
   case BIO_CTRL_PENDING:
     return 0;
diff --git a/rtc_base/opensslstreamadapter.cc b/rtc_base/opensslstreamadapter.cc
index 52a0b77..8f66c30 100644
--- a/rtc_base/opensslstreamadapter.cc
+++ b/rtc_base/opensslstreamadapter.cc
@@ -181,7 +181,6 @@
 static int stream_new(BIO* b) {
   b->shutdown = 0;
   b->init = 1;
-  b->num = 0;  // 1 means end-of-stream
   b->ptr = 0;
   return 1;
 }
@@ -202,8 +201,6 @@
   StreamResult result = stream->Read(out, outl, &read, &error);
   if (result == SR_SUCCESS) {
     return checked_cast<int>(read);
-  } else if (result == SR_EOS) {
-    b->num = 1;
   } else if (result == SR_BLOCK) {
     BIO_set_retry_read(b);
   }
@@ -234,8 +231,11 @@
   switch (cmd) {
     case BIO_CTRL_RESET:
       return 0;
-    case BIO_CTRL_EOF:
-      return b->num;
+    case BIO_CTRL_EOF: {
+      StreamInterface* stream = static_cast<StreamInterface*>(ptr);
+      // 1 means end-of-stream.
+      return (stream->GetState() == SS_CLOSED) ? 1 : 0;
+    }
     case BIO_CTRL_WPENDING:
     case BIO_CTRL_PENDING:
       return 0;