RawBridge.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2002-2009 Moxie Marlinspike
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 3 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  17. * USA
  18. */
  19. #ifndef __RAW_BRIDGE_H__
  20. #define __RAW_BRIDGE_H__
  21. #include <boost/bind.hpp>
  22. #include <boost/asio.hpp>
  23. #include <boost/shared_ptr.hpp>
  24. #include "Bridge.hpp"
  25. using namespace boost::asio;
  26. class RawBridge : public Bridge {
  27. private:
  28. int closed;
  29. boost::shared_ptr<ip::tcp::socket> clientSocket;
  30. ip::tcp::socket serverSocket;
  31. ip::tcp::endpoint destination;
  32. boost::asio::io_service &io_service;
  33. RawBridge(boost::shared_ptr<ip::tcp::socket> clientSocket,
  34. ip::tcp::endpoint& destination,
  35. boost::asio::io_service & io_service) :
  36. clientSocket(clientSocket), serverSocket(io_service),
  37. io_service(io_service), destination(destination), closed(0)
  38. {}
  39. void handleConnect(Bridge::ptr bridge, const boost::system::error_code &error) {
  40. if (!error) Bridge::shuttle(&(*clientSocket), &serverSocket);
  41. else close();
  42. }
  43. protected:
  44. virtual void processClientInput(char *buffer, int length) {}
  45. public:
  46. static ptr create(boost::shared_ptr<ip::tcp::socket> clientSocket,
  47. ip::tcp::endpoint& destination,
  48. boost::asio::io_service & io_service)
  49. {
  50. return ptr(new RawBridge(clientSocket, destination, io_service));
  51. }
  52. virtual ip::tcp::socket& getClientSocket() {
  53. return *clientSocket;
  54. }
  55. virtual ip::tcp::socket& getServerSocket() {
  56. return serverSocket;
  57. }
  58. virtual void shuttle() {
  59. serverSocket.async_connect(destination, boost::bind(&RawBridge::handleConnect,
  60. this,
  61. Bridge::getSmartPointer(),
  62. placeholders::error));
  63. }
  64. virtual void close() {
  65. if (!closed) closed = 1;
  66. if (clientSocket->is_open()) {
  67. clientSocket->cancel();
  68. clientSocket->close();
  69. }
  70. if (serverSocket.is_open()) {
  71. serverSocket.cancel();
  72. serverSocket.close();
  73. }
  74. }
  75. };
  76. #endif