Skip to content

Commit

Permalink
A dead TCP-connection should not be read/written to.
Browse files Browse the repository at this point in the history
  • Loading branch information
martinknafve committed Sep 26, 2014
1 parent 65e2829 commit a2c36eb
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 143 deletions.
11 changes: 4 additions & 7 deletions hmailserver/source/Server/Common/Application/ErrorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,22 @@ namespace HM
void
ErrorManager::ReportError(eSeverity iSeverity, int iErrorID, const String &sSource, const String &sDescription, const boost::system::system_error &error)
{
String formatted_message;
formatted_message.Format(_T("%s, Error code: %d, Message: %s"), sDescription, error.code().value(), String(error.what()));
String formatted_message
= Formatter::Format(_T("{0}, Error code: {1}, Message: {2}"), sDescription, error.code().value(), error.what());

ReportError(iSeverity, iErrorID, sSource, formatted_message);
}

void
ErrorManager::ReportError(eSeverity iSeverity, int iErrorID, const String &sSource, const String &sDescription, const std::exception &error)
{
String formatted_message;
formatted_message.Format(_T("%s, Error code: %d, Message: %s"), sDescription, String(error.what()));
String formatted_message
= Formatter::Format(_T("{0}, Message: {1}"), sDescription, error.what());

ReportError(iSeverity, iErrorID, sSource, formatted_message);
}





String
ErrorManager::GetWindowsErrorText(int windows_error_code)
{
Expand Down
16 changes: 10 additions & 6 deletions hmailserver/source/Server/Common/Application/ExceptionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "../Util/ExceptionLogger.h"

#include <boost/thread/thread.hpp>
#include "../TCPIP/DisconnectedException.h"

#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
Expand All @@ -26,12 +27,11 @@ namespace HM
// the shut down completes.
boost::this_thread::disable_interruption shutdown_temporarily_disabled;

LOG_DEBUG("Logging stack trace...");
LOG_DEBUG("Logging exception..");

ExceptionLogger::Log(dwExpCode, pExp);

LOG_DEBUG("Completed logging stack trace...");

LOG_DEBUG("Completed logging of exception...");

return EXCEPTION_EXECUTE_HANDLER;
}
Expand Down Expand Up @@ -59,17 +59,21 @@ namespace HM
{
func();
}
catch (thread_interrupted const&)
catch (thread_interrupted&)
{
// shutting down
}
catch (boost::system::system_error error)
catch (DisconnectedException&)
{
LOG_DEBUG("Connection was terminated - Client is disconnected.");
}
catch (boost::system::system_error& error)
{
ErrorManager::Instance()->ReportError(ErrorManager::High, 4208, "ExceptionHandler::Run", GetExceptionText(descriptive_name), error);

throw;
}
catch (std::exception const& error)
catch (std::exception& error)
{
String sErrorMessage =
Formatter::Format("An error occured while executing '{0}'", descriptive_name);
Expand Down
28 changes: 14 additions & 14 deletions hmailserver/source/Server/Common/TCPIP/CertificateVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include "CertificateVerifier.h"
#include "SocketConstants.h"

#include <boost/scope_exit.hpp>

#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
Expand Down Expand Up @@ -82,31 +80,26 @@ namespace HM
X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
BIO* bio = BIO_new(BIO_s_mem());

BOOST_SCOPE_EXIT(&bio) {
BIO_free(bio);
} BOOST_SCOPE_EXIT_END

// Convert the certificate from the internal structure to a DER structure in memory ('bio').
if (i2d_X509_bio(bio,cert) != 1)
{
ErrorManager::Instance()->ReportError(ErrorManager::Medium, 5512, "CertificateVerifier::operator()", "Failed to convert OpenSSL internal X509 to DER-format.");
BIO_free(bio);
return OverrideResult_(false);
}

// Read the cert from the BIO structure in memory to a char array.
int raw_size = BIO_pending(bio);
unsigned char *raw_certificate = new unsigned char[raw_size];

BOOST_SCOPE_EXIT(&raw_certificate) {
delete[] raw_certificate;
} BOOST_SCOPE_EXIT_END

int actual_read = BIO_read(bio, raw_certificate, raw_size);

if (raw_size != actual_read)
{
String errorMessage = Formatter::Format(_T("BIO_read returned an unexpected number of characters. Expected: {0}, Returned: {1}"), raw_size, actual_read);
ErrorManager::Instance()->ReportError(ErrorManager::Medium, 5513, "CertificateVerifier::operator()", errorMessage);
BIO_free(bio);
delete[] raw_certificate;
return OverrideResult_(false);
}

Expand All @@ -117,20 +110,22 @@ namespace HM
{
String errorMessage = Formatter::Format(_T("Call to CertCreateCertificateContext failed. Error: {0}"), (int) GetLastError());
ErrorManager::Instance()->ReportError(ErrorManager::Medium, 5513, "CertificateVerifier::operator()", errorMessage);
BIO_free(bio);
delete[] raw_certificate;
return OverrideResult_(false);
}

BOOST_SCOPE_EXIT(&context) {
CertFreeCertificateContext(context);
} BOOST_SCOPE_EXIT_END

String expected_host_name = host_name_;

int windows_error_code = 0;
if (VerifyCertificate_(context, expected_host_name.GetBuffer(-1), windows_error_code))
{
LOG_DEBUG(Formatter::Format("Certificate verification succeeded for session {0}.", session_id_));

BIO_free(bio);
delete[] raw_certificate;
CertFreeCertificateContext(context);

return OverrideResult_(true);
}
else
Expand All @@ -140,6 +135,11 @@ namespace HM
session_id_, host_name_, windows_error_code, windows_error_text);

LOG_DEBUG(formattedDebugMessage);

BIO_free(bio);
delete[] raw_certificate;
CertFreeCertificateContext(context);

return OverrideResult_(false);
}

Expand Down
21 changes: 21 additions & 0 deletions hmailserver/source/Server/Common/TCPIP/DisconnectedException.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2014 Martin Knafve / hMailServer.com.
// http://www.hmailserver.com

#pragma once

namespace HM
{
class DisconnectedException : public std::exception
{
public:

virtual const char* what() const
{
return "The client has been disconnected.";
}


private:

};
}
7 changes: 7 additions & 0 deletions hmailserver/source/Server/Common/TCPIP/SocketConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ namespace HM

};

enum ConnectionState
{
StateDisconnected = 0,
StateConnected = 1,
StatePendingDisconnect = 2
};

}
Loading

0 comments on commit a2c36eb

Please sign in to comment.