0

I am looking to asynchronously log the socket data that flows between the client and the server via a tcp proxy server.

I wasn't able to find enough details on tapping to the buffer and writing to a log sink. Any help ?

Below is the Boost:ASIO code that I have been able to successfully use/excecute -

  void handle_upstream_connect(const boost::system::error_code& error)
  {
     if (!error)
     {
        upstream_socket_.async_read_some(
             boost::asio::buffer(upstream_data_,max_data_length),
             boost::bind(&bridge::handle_upstream_read,
                  shared_from_this(),
                  boost::asio::placeholders::error,
                  boost::asio::placeholders::bytes_transferred));

        downstream_socket_.async_read_some(
             boost::asio::buffer(downstream_data_,max_data_length),
             boost::bind(&bridge::handle_downstream_read,
                  shared_from_this(),
                  boost::asio::placeholders::error,
                  boost::asio::placeholders::bytes_transferred));
     }
     else
        close();
  }

private:

  void handle_downstream_write(const boost::system::error_code& error)
  {
     if (!error)
     {
        upstream_socket_.async_read_some(
             boost::asio::buffer(upstream_data_,max_data_length),
             boost::bind(&bridge::handle_upstream_read,
                  shared_from_this(),
                  boost::asio::placeholders::error,
                  boost::asio::placeholders::bytes_transferred));
     }
     else
        close();
  }

  void handle_downstream_read(const boost::system::error_code& error,
                              const size_t& bytes_transferred)
  {
     if (!error)
     {
        async_write(upstream_socket_,
              boost::asio::buffer(downstream_data_,bytes_transferred),
              boost::bind(&bridge::handle_upstream_write,
                    shared_from_this(),
                    boost::asio::placeholders::error));
     }
     else
        close();
  }

  void handle_upstream_write(const boost::system::error_code& error)
  {
     if (!error)
     {
        downstream_socket_.async_read_some(
             boost::asio::buffer(downstream_data_,max_data_length),
             boost::bind(&bridge::handle_downstream_read,
                  shared_from_this(),
                  boost::asio::placeholders::error,
                  boost::asio::placeholders::bytes_transferred));
     }
     else
        close();
  }

  void handle_upstream_read(const boost::system::error_code& error,
                            const size_t& bytes_transferred)
  {
     if (!error)
     {
        async_write(downstream_socket_,
             boost::asio::buffer(upstream_data_,bytes_transferred),
             boost::bind(&bridge::handle_downstream_write,
                  shared_from_this(),
                  boost::asio::placeholders::error));
     }
     else
        close();
  }
VidhyaS
  • 31
  • 3
  • You do have `upstream_data_` and `downstream_data_`. Whats the problem with tapping data ? – Arunmu Feb 21 '17 at 04:13
  • The problem is with the "async" piece of it. Any, async_write API internally writes the "downstream_data_" or the "upstream_data_" as a buffer and writes in chunks. I was trying to see if there are any options to hook to the buffer or log the socket data with a call back API. – VidhyaS Feb 21 '17 at 15:24
  • Probably I am not getting your requirement. But what prohibits you from doing the same in the completion handler of `async_write` ? Are you worried about data in the buffer getting overwritten or something before you log ? – Arunmu Feb 21 '17 at 15:51
  • @RaviK: I'm not sure I understand your question, and I think you should update it with a more specific problem description. If the problem is that you don't know which of the two buffers to log in the completion handler, you can add it to the handler arguments by means of `boost::bind`. If the problem is that you don't know how to actually log the buffer contents, then please specify that. – Andrey Semashev Feb 21 '17 at 15:57
  • Apologies for not being clear. As far as I have understood, the completion handler for async_write is already specified. I am invoking the below function - but it is logging the data repeatedly - void log_downstream_write(size_t bytes_transferred) { boost::mutex::scoped_lock lock(mutex_); BOOST_LOG(m_logger) << upstream_data_; } – VidhyaS Feb 21 '17 at 16:44
  • @RaviK It shouldn't be too hard to get a small code which reproduces your issue. Difficult to say what you are actually doing wrong without seeing the code. – Arunmu Feb 22 '17 at 15:41
  • @RaviK: Is the problem that `upstream_data_` is logged multiple times? Why do you ignore `bytes_transferred`? If you want to log the full transferred data once, you should either log it before sending, or log it after it has been fully sent. – Andrey Semashev Feb 27 '17 at 11:03
  • Thanks for inputs. I have got it to work. – VidhyaS Feb 27 '17 at 21:54

0 Answers0