szaszm commented on code in PR #1599:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1599#discussion_r1301181584
##########
libminifi/include/utils/net/AsioSocketUtils.h:
##########
@@ -63,6 +64,64 @@ asio::awaitable<std::tuple<std::error_code>>
handshake(SslSocket& socket, asio::
asio::ssl::context getSslContext(const controllers::SSLContextService&
ssl_context_service, asio::ssl::context::method ssl_context_method =
asio::ssl::context::tlsv12_client);
+
+struct SocketData {
+ std::string host = "localhost";
+ int port = -1;
+ std::shared_ptr<minifi::controllers::SSLContextService> ssl_context_service;
+};
+
+class AsioSocketConnection : public io::BaseStream {
+ public:
+ explicit AsioSocketConnection(SocketData socket_data);
+ int initialize() override;
+ size_t read(std::span<std::byte> out_buffer) override {
+ gsl_Expects(stream_);
+ return stream_->read(out_buffer);
+ }
+ size_t write(const uint8_t *in_buffer, size_t len) override {
+ gsl_Expects(stream_);
+ return stream_->write(in_buffer, len);
+ }
+
+ void setInterface(const std::string& local_network_interface) {
+ local_network_interface_ = local_network_interface;
+ }
+
+ private:
+ template<typename SocketType>
+ bool bindToLocalInterface(SocketType& socket) {
+ if (local_network_interface_.empty()) {
+ return true;
+ }
+
+ asio::ip::tcp::endpoint
local_endpoint(asio::ip::address::from_string(local_network_interface_), 0);
+ asio::error_code err;
+ socket.open(local_endpoint.protocol(), err);
+ if (err) {
+ logger_->log_error("Failed to open socket on network interface '%s' with
the following message: '%s'", local_network_interface_, err.message());
+ return false;
+ }
+ socket.set_option(asio::ip::tcp::socket::reuse_address(true));
Review Comment:
What's the reason for this `reuse_address(true)` option? I think normally
it's used to be able to quickly restart a server program that binds to the same
address and port as the old process (of the same software) did. We should avoid
`bind` and `reuse_address(true)` for client connections: let closed connections
linger in the TIME_WAIT state, and let the dynamic binding of ports assign
another random port to the client process.
##########
libminifi/src/utils/net/AsioSocketUtils.cpp:
##########
@@ -44,4 +47,66 @@ asio::ssl::context getSslContext(const
controllers::SSLContextService& ssl_conte
return ssl_context;
}
+AsioSocketConnection::AsioSocketConnection(SocketData socket_data) :
socket_data_(std::move(socket_data)) {
+}
+
+int AsioSocketConnection::initialize() {
+ bool result = false;
+ if (socket_data_.ssl_context_service) {
+ result = connectTcpSocketOverSsl();
+ } else {
+ result = connectTcpSocket();
+ }
+ return result ? 0 : -1;
+}
+
+bool AsioSocketConnection::connectTcpSocketOverSsl() {
+ auto ssl_context =
utils::net::getSslContext(*socket_data_.ssl_context_service);
+ asio::ssl::stream<asio::ip::tcp::socket> socket(io_context_, ssl_context);
+
+ bindToLocalInterface(socket.lowest_layer());
Review Comment:
As mentioned in the previous comment: client sockets (that initiate a
connection) should probably not bind to anything.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]