Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/compile config delay sending 226 #63

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions fineftp-server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ target_compile_options(${PROJECT_NAME} PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:
/W4>)

# The below is used to configure an optional delay when sending the 226 response code when a file has been fetched.
# The background for this option is that an FTP client implementation has been observed to close the data connection
# as soon as it receives the 226 status code - even though it hasn't received/ all data, yet. To improve
# interoperability with such buggy clients, sending of the 226 status code can be delayed a bit. The delay is specified
# in milliseconds. If the delay is 0, no delay is introduced.
set(FINEFTP_SERVER_DELAY_226_RESP_MS 0 CACHE STRING
"An optional delay (in ms) for the 226 response when a file has been fetched. Used to improve interoperability with buggy clients.")
target_compile_definitions(${PROJECT_NAME} PRIVATE DELAY_226_RESP_MS=${FINEFTP_SERVER_DELAY_226_RESP_MS})

# Add own public include directory
target_include_directories(${PROJECT_NAME}
Expand Down
32 changes: 29 additions & 3 deletions fineftp-server/src/ftp_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <algorithm>
#include <cassert> // assert
#include <cctype> // std::iscntrl, toupper
#include <chrono>
#include <cstddef>
#include <cstdio>
#include <fstream>
Expand Down Expand Up @@ -47,6 +48,7 @@ namespace fineftp
, ftp_working_directory_("/")
, data_acceptor_ (io_service)
, data_socket_strand_ (io_service)
, timer_ (io_service)
{
}

Expand Down Expand Up @@ -454,9 +456,9 @@ namespace fineftp
// Form reply string
std::stringstream stream;
stream << "(";
for (const char byte : ip_bytes)
for (const auto byte : ip_bytes)
{
stream << static_cast<int>(byte) << ",";
stream << static_cast<unsigned int>(byte) << ",";
}
stream << ((port >> 8) & 0xff) << "," << (port & 0xff) << ")";

Expand Down Expand Up @@ -1302,7 +1304,31 @@ namespace fineftp
}
else
{
me->sendFtpMessage(FtpReplyCode::CLOSING_DATA_CONNECTION, "Done");
// Ugly work-around:
// An FTP client implementation has been observed to close the data connection
// as soon as it receives the 226 status code - even though it hasn't received
// all data, yet. To improve interoperability with such buggy clients, sending
// of the 226 status code can be delayed a bit. The delay is defined through a
// preprocessor definition. If the delay is 0, no delay is introduced at all.
#if (0 == DELAY_226_RESP_MS)
me->sendFtpMessage(FtpReplyCode::CLOSING_DATA_CONNECTION, "Done");
#else
// Close Data Socket properly
{
asio::error_code ec;
data_socket->shutdown(asio::socket_base::shutdown_both, ec);
data_socket->close(ec);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bjuulp: This must always be executed, right? I think the current implementation does not close the data socket at all, after sending a file...

My old implementation did close the socket, but the new memory-mapped file implementation does not use the writeDataToSocket function anymore, which would close the socket. So I think this bug was introduced with the memory-mapped file implementation. Funny, that curl doesn't complain about that.

Edit: Ah, I think the shared-pointer goes out of scope and the destructor closes the socket, which saved us. I still think we should explicitly shutdown the connection properly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I'll add that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've now tested what happens if I explicitly close the socket and set the delay to 0. This still triggers the bug in the old lwftp implementation. So, the only thing I've done in the latest commit is to move the explicit connection closure so that it is done irrespective of the presence or absence of the delay.

me->timer_.expires_after(std::chrono::milliseconds{DELAY_226_RESP_MS});
me->timer_.async_wait(me->data_socket_strand_.wrap([me](const asio::error_code& ec)
{
if (ec != asio::error::operation_aborted)
{
me->sendFtpMessage(FtpReplyCode::CLOSING_DATA_CONNECTION, "Done");
}
}));
#endif
}
});
}
Expand Down
2 changes: 2 additions & 0 deletions fineftp-server/src/ftp_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,7 @@ namespace fineftp
asio::io_service::strand data_socket_strand_;
std::weak_ptr<asio::ip::tcp::socket> data_socket_weakptr_;
std::deque<std::shared_ptr<std::vector<char>>> data_buffer_;

asio::steady_timer timer_;
};
}