Add a pair of util functions for getting and setting the BIO fd
authorChristopher Dykes <cdykes@fb.com>
Wed, 17 Aug 2016 21:42:29 +0000 (14:42 -0700)
committerFacebook Github Bot 6 <facebook-github-bot-6-bot@fb.com>
Wed, 17 Aug 2016 21:53:26 +0000 (14:53 -0700)
Summary: Because we need to translate them between sockets and file descriptors when we're on Windows.

Reviewed By: yfeldblum

Differential Revision: D3724802

fbshipit-source-id: 07fff6e1bec7b9b90e0d39fd98441466a746b7f7

folly/io/async/AsyncSSLSocket.cpp
folly/io/async/ssl/OpenSSLUtils.cpp
folly/io/async/ssl/OpenSSLUtils.h

index c16e6fb6c66393045e3d298d8503297b689aaedc..5efa402e7e7001684307fc192c43554ee4decaef 100644 (file)
@@ -699,7 +699,7 @@ bool AsyncSSLSocket::setupSSLBio() {
   }
 
   OpenSSLUtils::setBioAppData(wb, this);
-  BIO_set_fd(wb, fd_, BIO_NOCLOSE);
+  OpenSSLUtils::setBioFd(wb, fd_, BIO_NOCLOSE);
   SSL_set_bio(ssl_, wb, wb);
   return true;
 }
@@ -1590,8 +1590,8 @@ int AsyncSSLSocket::bioWrite(BIO* b, const char* in, int inl) {
   flags |= MSG_NOSIGNAL;
 #endif
 
-  auto result =
-      tsslSock->sendSocketMessage(BIO_get_fd(b, nullptr), &msg, flags);
+  auto result = tsslSock->sendSocketMessage(
+      OpenSSLUtils::getBioFd(b, nullptr), &msg, flags);
   BIO_clear_retry_flags(b);
   if (!result.exception && result.writeReturn <= 0) {
     if (OpenSSLUtils::getBioShouldRetryWrite(result.writeReturn)) {
index 01aa8548a4ec0d198dd450979f2e37285adc6815..575583205b1a3a1b670d8b277215e2a1ad5dfb7e 100644 (file)
@@ -180,6 +180,27 @@ void OpenSSLUtils::setCustomBioMethod(BIO* b, BIO_METHOD* meth) {
 #endif
 }
 
+int OpenSSLUtils::getBioFd(BIO* b, int* fd) {
+#ifdef _WIN32
+  int ret = portability::sockets::socket_to_fd((SOCKET)BIO_get_fd(b, fd));
+  if (fd != nullptr) {
+    *fd = ret;
+  }
+  return ret;
+#else
+  return BIO_get_fd(b, fd);
+#endif
+}
+
+void OpenSSLUtils::setBioFd(BIO* b, int fd, int flags) {
+#ifdef _WIN32
+  SOCKET sock = portability::sockets::fd_to_socket(fd);
+#else
+  int sock = fd;
+#endif
+  BIO_set_fd(b, sock, flags);
+}
+
 } // ssl
 } // folly
 
index 252785e358d0f2524becd8201e7ac12894bc0e32..204cb431895908027e4a98269944ac91facdab2f 100644 (file)
@@ -69,6 +69,8 @@ class OpenSSLUtils {
   static void setBioAppData(BIO* b, void* ptr);
   static void* getBioAppData(BIO* b);
   static void setCustomBioMethod(BIO*, BIO_METHOD*);
+  static int getBioFd(BIO* b, int* fd);
+  static void setBioFd(BIO* b, int fd, int flags);
 };
 
 } // ssl