From 2b6ac135fa7071290289741c9e35747bb9f1012f Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Sat, 1 Mar 2014 10:42:48 +0000 Subject: Make Protocol take control of the socket object passed in. We pass a std::auto_ptr to every Protocol subclass when we construct it, and it takes control of this object. This reduces the risk of: * accidentally reusing the same SocketStream for multiple Protocols (it happened to me in testbackupstore); * holding onto a reference to the SocketStream; * allowing a locally-scoped SocketStream to go out of scope and be released while still being referenced by a live Protocol. --- bin/bbstored/BackupStoreDaemon.cpp | 25 +++++++++++++------------ bin/bbstored/BackupStoreDaemon.h | 6 +++--- 2 files changed, 16 insertions(+), 15 deletions(-) (limited to 'bin/bbstored') diff --git a/bin/bbstored/BackupStoreDaemon.cpp b/bin/bbstored/BackupStoreDaemon.cpp index 2649e0a2..8fddf125 100644 --- a/bin/bbstored/BackupStoreDaemon.cpp +++ b/bin/bbstored/BackupStoreDaemon.cpp @@ -272,11 +272,11 @@ void BackupStoreDaemon::Run() // Created: 2003/08/20 // // -------------------------------------------------------------------------- -void BackupStoreDaemon::Connection(SocketStreamTLS &rStream) +void BackupStoreDaemon::Connection(std::auto_ptr apStream) { try { - Connection2(rStream); + Connection2(apStream); } catch(BoxException &e) { @@ -304,10 +304,10 @@ void BackupStoreDaemon::Connection(SocketStreamTLS &rStream) // Created: 2006/11/12 // // -------------------------------------------------------------------------- -void BackupStoreDaemon::Connection2(SocketStreamTLS &rStream) +void BackupStoreDaemon::Connection2(std::auto_ptr apStream) { // Get the common name from the certificate - std::string clientCommonName(rStream.GetPeerCommonName()); + std::string clientCommonName(apStream->GetPeerCommonName()); // Log the name BOX_INFO("Client certificate CN: " << clientCommonName); @@ -346,7 +346,8 @@ void BackupStoreDaemon::Connection2(SocketStreamTLS &rStream) } // Handle a connection with the backup protocol - BackupProtocolServer server(rStream); + std::auto_ptr apPlainStream(apStream); + BackupProtocolServer server(apPlainStream); server.SetLogToSysLog(mExtendedLogging); server.SetTimeout(BACKUP_STORE_TIMEOUT); try @@ -355,22 +356,22 @@ void BackupStoreDaemon::Connection2(SocketStreamTLS &rStream) } catch(...) { - LogConnectionStats(id, context.GetAccountName(), rStream); + LogConnectionStats(id, context.GetAccountName(), server); throw; } - LogConnectionStats(id, context.GetAccountName(), rStream); + LogConnectionStats(id, context.GetAccountName(), server); context.CleanUp(); } void BackupStoreDaemon::LogConnectionStats(uint32_t accountId, - const std::string& accountName, const SocketStreamTLS &s) + const std::string& accountName, const BackupProtocolServer &server) { // Log the amount of data transferred BOX_NOTICE("Connection statistics for " << BOX_FORMAT_ACCOUNT(accountId) << " " "(name=" << accountName << "):" - " IN=" << s.GetBytesRead() << - " OUT=" << s.GetBytesWritten() << - " NET_IN=" << (s.GetBytesRead() - s.GetBytesWritten()) << - " TOTAL=" << (s.GetBytesRead() + s.GetBytesWritten())); + " IN=" << server.GetBytesRead() << + " OUT=" << server.GetBytesWritten() << + " NET_IN=" << (server.GetBytesRead() - server.GetBytesWritten()) << + " TOTAL=" << (server.GetBytesRead() + server.GetBytesWritten())); } diff --git a/bin/bbstored/BackupStoreDaemon.h b/bin/bbstored/BackupStoreDaemon.h index ce538477..a2dab5e5 100644 --- a/bin/bbstored/BackupStoreDaemon.h +++ b/bin/bbstored/BackupStoreDaemon.h @@ -52,8 +52,8 @@ protected: virtual void Run(); - virtual void Connection(SocketStreamTLS &rStream); - void Connection2(SocketStreamTLS &rStream); + virtual void Connection(std::auto_ptr apStream); + void Connection2(std::auto_ptr apStream); virtual const char *DaemonName() const; virtual std::string DaemonBanner() const; @@ -64,7 +64,7 @@ protected: void HousekeepingProcess(); void LogConnectionStats(uint32_t accountId, - const std::string& accountName, const SocketStreamTLS &s); + const std::string& accountName, const BackupProtocolServer &server); public: // HousekeepingInterface implementation -- cgit v1.2.3