From d14bc294df554664f990fa1105a3832a5ef1b40d Mon Sep 17 00:00:00 2001
From: Garrett Holmstrom <gholms@devzero.com>
Date: Mon, 11 Jan 2021 19:44:20 -0800
Subject: [PATCH 5/5] Add new error category and constant for
 ssl::error::stream_truncated

Backports 84e840390bffb2ab409cc7d1eaaf427a26f093df
---
 boost/asio/ssl/detail/impl/engine.ipp |  8 ++-----
 boost/asio/ssl/error.hpp              | 34 +++++++++++++++++++++++++++
 boost/asio/ssl/impl/error.ipp         | 33 +++++++++++++++++++++++++-
 3 files changed, 68 insertions(+), 7 deletions(-)

diff --git a/boost/asio/ssl/detail/impl/engine.ipp b/boost/asio/ssl/detail/impl/engine.ipp
index 9fdd3836..980ce19e 100644
--- a/boost/asio/ssl/detail/impl/engine.ipp
+++ b/boost/asio/ssl/detail/impl/engine.ipp
@@ -203,9 +203,7 @@ const boost::system::error_code& engine::map_error_code(
   // If there's data yet to be read, it's an error.
   if (BIO_wpending(ext_bio_))
   {
-    ec = boost::system::error_code(
-        ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ),
-        boost::asio::error::get_ssl_category());
+    ec = boost::asio::ssl::error::stream_truncated;
     return ec;
   }
 
@@ -219,9 +217,7 @@ const boost::system::error_code& engine::map_error_code(
   // Otherwise, the peer should have negotiated a proper shutdown.
   if ((::SSL_get_shutdown(ssl_) & SSL_RECEIVED_SHUTDOWN) == 0)
   {
-    ec = boost::system::error_code(
-        ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ),
-        boost::asio::error::get_ssl_category());
+    ec = boost::asio::ssl::error::stream_truncated;
   }
 
   return ec;
diff --git a/boost/asio/ssl/error.hpp b/boost/asio/ssl/error.hpp
index 3a4b1358..e9557060 100644
--- a/boost/asio/ssl/error.hpp
+++ b/boost/asio/ssl/error.hpp
@@ -26,6 +26,7 @@ namespace error {
 
 enum ssl_errors
 {
+  // Error numbers are those produced by openssl.
 };
 
 extern BOOST_ASIO_DECL
@@ -35,6 +36,23 @@ static const boost::system::error_category& ssl_category
   = boost::asio::error::get_ssl_category();
 
 } // namespace error
+namespace ssl {
+namespace error {
+
+enum stream_errors
+{
+  /// The underlying stream closed before the ssl stream gracefully shut down.
+  stream_truncated = 1
+};
+
+extern BOOST_ASIO_DECL
+const boost::system::error_category& get_stream_category();
+
+static const boost::system::error_category& stream_category
+  = boost::asio::ssl::error::get_stream_category();
+
+} // namespace error
+} // namespace ssl
 } // namespace asio
 } // namespace boost
 
@@ -46,6 +64,11 @@ template<> struct is_error_code_enum<boost::asio::error::ssl_errors>
   static const bool value = true;
 };
 
+template<> struct is_error_code_enum<boost::asio::ssl::error::stream_errors>
+{
+  static const bool value = true;
+};
+
 } // namespace system
 } // namespace boost
 
@@ -60,6 +83,17 @@ inline boost::system::error_code make_error_code(ssl_errors e)
 }
 
 } // namespace error
+namespace ssl {
+namespace error {
+
+inline boost::system::error_code make_error_code(stream_errors e)
+{
+  return boost::system::error_code(
+      static_cast<int>(e), get_stream_category());
+}
+
+} // namespace error
+} // namespace ssl
 } // namespace asio
 } // namespace boost
 
diff --git a/boost/asio/ssl/impl/error.ipp b/boost/asio/ssl/impl/error.ipp
index e411a4d3..c171ba14 100644
--- a/boost/asio/ssl/impl/error.ipp
+++ b/boost/asio/ssl/impl/error.ipp
@@ -24,7 +24,6 @@
 namespace boost {
 namespace asio {
 namespace error {
-
 namespace detail {
 
 class ssl_category : public boost::system::error_category
@@ -51,6 +50,38 @@ const boost::system::error_category& get_ssl_category()
 }
 
 } // namespace error
+namespace ssl {
+namespace error {
+namespace detail {
+
+class stream_category : public boost::system::error_category
+{
+public:
+  const char* name() const BOOST_ASIO_ERROR_CATEGORY_NOEXCEPT
+  {
+    return "asio.ssl.stream";
+  }
+
+  std::string message(int value) const
+  {
+    switch (value)
+    {
+    case stream_truncated: return "stream truncated";
+    default: return "asio.ssl.stream error";
+    }
+  }
+};
+
+} // namespace detail
+
+const boost::system::error_category& get_stream_category()
+{
+  static detail::stream_category instance;
+  return instance;
+}
+
+} // namespace error
+} // namespace ssl
 } // namespace asio
 } // namespace boost
 
-- 
2.29.0

