From dab1609bafed6f361d16dbb866f790932fc28696 Mon Sep 17 00:00:00 2001 From: czeidler Date: Sun, 29 Apr 2012 16:50:25 +1200 Subject: [PATCH] Use BSecureSocket and BSocket instead of ServerConnection. * When sending a new command, drain all leftover data and not only 1025 chars. --- .../inbound_protocols/pop3/Jamfile | 4 +- .../inbound_protocols/pop3/pop3.cpp | 54 +++++++++++-------- .../mail_daemon/inbound_protocols/pop3/pop3.h | 7 +-- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/src/add-ons/mail_daemon/inbound_protocols/pop3/Jamfile b/src/add-ons/mail_daemon/inbound_protocols/pop3/Jamfile index fb2a9d9af7..527a778b0e 100644 --- a/src/add-ons/mail_daemon/inbound_protocols/pop3/Jamfile +++ b/src/add-ons/mail_daemon/inbound_protocols/pop3/Jamfile @@ -32,8 +32,8 @@ AddResources POP3 : POP3.rdef ; Addon POP3 : $(sources) - : be libmail.so $(HAIKU_LOCALE_LIBS) $(HAIKU_OPENSSL_LIBS) - $(TARGET_LIBSUPC++) $(TARGET_NETWORK_LIBS) + : be libbnetapi.so libmail.so $(HAIKU_LOCALE_LIBS) + $(HAIKU_OPENSSL_LIBS) $(TARGET_LIBSUPC++) $(TARGET_NETWORK_LIBS) ; Package haiku-maildaemon-cvs : diff --git a/src/add-ons/mail_daemon/inbound_protocols/pop3/pop3.cpp b/src/add-ons/mail_daemon/inbound_protocols/pop3/pop3.cpp index 7474db4321..7ad21e7f63 100644 --- a/src/add-ons/mail_daemon/inbound_protocols/pop3/pop3.cpp +++ b/src/add-ons/mail_daemon/inbound_protocols/pop3/pop3.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -63,7 +64,8 @@ POP3Protocol::POP3Protocol(BMailAccountSettings* settings) : InboundProtocol(settings), fNumMessages(-1), - fMailDropSize(0) + fMailDropSize(0), + fServerConnection(NULL) { printf("POP3Protocol::POP3Protocol(BMailAccountSettings* settings)\n"); fSettings = fAccountSettings.InboundSettings().Settings(); @@ -102,7 +104,7 @@ POP3Protocol::Connect() delete[] password; if (error != B_OK) - fServerConnection.Disconnect(); + fServerConnection->Disconnect(); return error; } @@ -112,7 +114,9 @@ POP3Protocol::Disconnect() { SendCommand("QUIT" CRLF); - fServerConnection.Disconnect(); + fServerConnection->Disconnect(); + delete fServerConnection; + fServerConnection = NULL; return B_OK; } @@ -366,19 +370,26 @@ POP3Protocol::Open(const char* server, int port, int) return B_NAME_NOT_FOUND; } - status_t status = B_ERROR; - if (fUseSSL) - status = fServerConnection.ConnectSSL(server, port); - else - status = fServerConnection.ConnectSocket(server, port); - if (status != B_OK) - return status; + delete fServerConnection; + fServerConnection = NULL; + if (fUseSSL) { + fServerConnection = new(std::nothrow) BSecureSocket( + BNetworkAddress(server, port)); + } else { + fServerConnection = new(std::nothrow) BSocket(BNetworkAddress( + server, port)); + } + + if (fServerConnection == NULL) + return B_NO_MEMORY; + if (fServerConnection->InitCheck() != B_OK) + return fServerConnection->InitCheck(); BString line; status_t err = ReceiveLine(line); if (err < 0) { - fServerConnection.Disconnect(); + fServerConnection->Disconnect(); error_msg << ": " << strerror(err); ShowError(error_msg.String()); return B_ERROR; @@ -392,7 +403,7 @@ POP3Protocol::Open(const char* server, int port, int) error_msg << B_TRANSLATE(": No reply.\n"); ShowError(error_msg.String()); - fServerConnection.Disconnect(); + fServerConnection->Disconnect(); return B_ERROR; } @@ -627,7 +638,8 @@ POP3Protocol::RetrieveInternal(const char *command, int32 message, return B_ERROR; while (cont) { - status_t result = fServerConnection.WaitForData(POP3_RETRIEVAL_TIMEOUT); + status_t result = fServerConnection->WaitForReadable( + POP3_RETRIEVAL_TIMEOUT); if (result == B_TIMED_OUT) { // No data available, even after waiting a minute. fLog = "POP3 timeout - no data received after a long wait."; @@ -636,7 +648,7 @@ POP3Protocol::RetrieveInternal(const char *command, int32 message, if (amountToReceive > bufSize - 1 - amountInBuffer) amountToReceive = bufSize - 1 - amountInBuffer; - amountReceived = fServerConnection.Read(buf + amountInBuffer, + amountReceived = fServerConnection->Read(buf + amountInBuffer, amountToReceive); if (amountReceived < 0) { @@ -752,7 +764,8 @@ POP3Protocol::ReceiveLine(BString &line) line = ""; - status_t result = fServerConnection.WaitForData(POP3_RETRIEVAL_TIMEOUT); + status_t result = fServerConnection->WaitForReadable( + POP3_RETRIEVAL_TIMEOUT); if (result == B_TIMED_OUT) return errno; @@ -761,7 +774,7 @@ POP3Protocol::ReceiveLine(BString &line) int32 bytesReceived; uint8 c = 0; - bytesReceived = fServerConnection.Read((char*)&c, 1); + bytesReceived = fServerConnection->Read((char*)&c, 1); if (bytesReceived < 0) return errno; @@ -793,12 +806,11 @@ POP3Protocol::SendCommand(const char* cmd) // don't misinterrpret responses from previous commands (that got left over // due to bugs) as being from this command. - status_t result = fServerConnection.WaitForData(1000); - if (result == B_OK) { + while (fServerConnection->WaitForReadable(1000) == B_OK) { int amountReceived; - char tempString [1025]; + char tempString [1024]; - amountReceived = fServerConnection.Read(tempString, + amountReceived = fServerConnection->Read(tempString, sizeof(tempString) - 1); if (amountReceived < 0) return errno; @@ -810,7 +822,7 @@ POP3Protocol::SendCommand(const char* cmd) // break; } - if (fServerConnection.Write(cmd, ::strlen(cmd)) < 0) { + if (fServerConnection->Write(cmd, ::strlen(cmd)) < 0) { fLog = strerror(errno); printf("POP3Protocol::SendCommand Send \"%s\" failed, code %d: %s\n", cmd, errno, fLog.String()); diff --git a/src/add-ons/mail_daemon/inbound_protocols/pop3/pop3.h b/src/add-ons/mail_daemon/inbound_protocols/pop3/pop3.h index 090b98e161..785d3275a8 100644 --- a/src/add-ons/mail_daemon/inbound_protocols/pop3/pop3.h +++ b/src/add-ons/mail_daemon/inbound_protocols/pop3/pop3.h @@ -12,8 +12,6 @@ #include #include -#include "ServerConnection.h" - #include #include #include @@ -25,6 +23,9 @@ #include +class BSocket; + + class POP3Protocol : public InboundProtocol { public: POP3Protocol(BMailAccountSettings* settings); @@ -79,7 +80,7 @@ private: BString fDestinationDir; int32 fFetchBodyLimit; - ServerConnection fServerConnection; + BSocket* fServerConnection; bool fUseSSL; };