Browse Source

use shared_ptr for socket in I2PTunnelConnection

pull/177/head
orignal 10 years ago
parent
commit
e72eb35cc2
  1. 11
      BOB.cpp
  2. 2
      BOB.h
  3. 10
      HTTPProxy.cpp
  4. 2
      HTTPProxy.h
  5. 13
      I2PService.cpp
  6. 4
      I2PService.h
  7. 15
      I2PTunnel.cpp
  8. 10
      I2PTunnel.h
  9. 7
      SOCKS.cpp
  10. 2
      SOCKS.h

11
BOB.cpp

@ -34,7 +34,7 @@ namespace client
void BOBI2PInboundTunnel::Accept () void BOBI2PInboundTunnel::Accept ()
{ {
auto receiver = new AddressReceiver (); auto receiver = new AddressReceiver ();
receiver->socket = new boost::asio::ip::tcp::socket (GetService ()); receiver->socket = std::make_shared<boost::asio::ip::tcp::socket> (GetService ());
m_Acceptor.async_accept (*receiver->socket, std::bind (&BOBI2PInboundTunnel::HandleAccept, this, m_Acceptor.async_accept (*receiver->socket, std::bind (&BOBI2PInboundTunnel::HandleAccept, this,
std::placeholders::_1, receiver)); std::placeholders::_1, receiver));
} }
@ -47,11 +47,8 @@ namespace client
ReceiveAddress (receiver); ReceiveAddress (receiver);
} }
else else
{
delete receiver->socket;
delete receiver; delete receiver;
} }
}
void BOBI2PInboundTunnel::ReceiveAddress (AddressReceiver * receiver) void BOBI2PInboundTunnel::ReceiveAddress (AddressReceiver * receiver)
{ {
@ -68,7 +65,6 @@ namespace client
if (ecode) if (ecode)
{ {
LogPrint ("BOB inbound tunnel read error: ", ecode.message ()); LogPrint ("BOB inbound tunnel read error: ", ecode.message ());
delete receiver->socket;
delete receiver; delete receiver;
} }
else else
@ -86,7 +82,6 @@ namespace client
if (!context.GetAddressBook ().GetIdentHash (receiver->buffer, ident)) if (!context.GetAddressBook ().GetIdentHash (receiver->buffer, ident))
{ {
LogPrint (eLogError, "BOB address ", receiver->buffer, " not found"); LogPrint (eLogError, "BOB address ", receiver->buffer, " not found");
delete receiver->socket;
delete receiver; delete receiver;
return; return;
} }
@ -105,7 +100,6 @@ namespace client
else else
{ {
LogPrint ("BOB missing inbound address "); LogPrint ("BOB missing inbound address ");
delete receiver->socket;
delete receiver; delete receiver;
} }
} }
@ -125,7 +119,6 @@ namespace client
else else
LogPrint ("LeaseSet for BOB inbound destination not found"); LogPrint ("LeaseSet for BOB inbound destination not found");
} }
delete receiver->socket;
delete receiver; delete receiver;
} }
@ -167,7 +160,7 @@ namespace client
{ {
if (stream) if (stream)
{ {
auto conn = std::make_shared<I2PTunnelConnection> (this, stream, new boost::asio::ip::tcp::socket (GetService ()), m_Endpoint, m_IsQuiet); auto conn = std::make_shared<I2PTunnelConnection> (this, stream, std::make_shared<boost::asio::ip::tcp::socket> (GetService ()), m_Endpoint, m_IsQuiet);
AddHandler (conn); AddHandler (conn);
conn->Connect (); conn->Connect ();
} }

2
BOB.h

@ -57,7 +57,7 @@ namespace client
{ {
struct AddressReceiver struct AddressReceiver
{ {
boost::asio::ip::tcp::socket * socket; std::shared_ptr<boost::asio::ip::tcp::socket> socket;
char buffer[BOB_COMMAND_BUFFER_SIZE + 1]; // for destination base64 address char buffer[BOB_COMMAND_BUFFER_SIZE + 1]; // for destination base64 address
uint8_t * data; uint8_t * data;
size_t dataLen, bufferOffset; size_t dataLen, bufferOffset;

10
HTTPProxy.cpp

@ -44,7 +44,7 @@ namespace proxy
void HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream); void HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream);
uint8_t m_http_buff[http_buffer_size]; uint8_t m_http_buff[http_buffer_size];
boost::asio::ip::tcp::socket * m_sock; std::shared_ptr<boost::asio::ip::tcp::socket> m_sock;
std::string m_request; //Data left to be sent std::string m_request; //Data left to be sent
std::string m_url; //URL std::string m_url; //URL
std::string m_method; //Method std::string m_method; //Method
@ -56,7 +56,7 @@ namespace proxy
public: public:
HTTPProxyHandler(HTTPProxyServer * parent, boost::asio::ip::tcp::socket * sock) : HTTPProxyHandler(HTTPProxyServer * parent, std::shared_ptr<boost::asio::ip::tcp::socket> sock) :
I2PServiceHandler(parent), m_sock(sock) I2PServiceHandler(parent), m_sock(sock)
{ EnterState(GET_METHOD); } { EnterState(GET_METHOD); }
~HTTPProxyHandler() { Terminate(); } ~HTTPProxyHandler() { Terminate(); }
@ -77,10 +77,10 @@ namespace proxy
void HTTPProxyHandler::Terminate() { void HTTPProxyHandler::Terminate() {
if (Kill()) return; if (Kill()) return;
if (m_sock) { if (m_sock)
{
LogPrint(eLogDebug,"--- HTTP Proxy close sock"); LogPrint(eLogDebug,"--- HTTP Proxy close sock");
m_sock->close(); m_sock->close();
delete m_sock;
m_sock = nullptr; m_sock = nullptr;
} }
Done(shared_from_this()); Done(shared_from_this());
@ -290,7 +290,7 @@ namespace proxy
{ {
} }
std::shared_ptr<i2p::client::I2PServiceHandler> HTTPProxyServer::CreateHandler(boost::asio::ip::tcp::socket * socket) std::shared_ptr<i2p::client::I2PServiceHandler> HTTPProxyServer::CreateHandler(std::shared_ptr<boost::asio::ip::tcp::socket> socket)
{ {
return std::make_shared<HTTPProxyHandler> (this, socket); return std::make_shared<HTTPProxyHandler> (this, socket);
} }

2
HTTPProxy.h

@ -21,7 +21,7 @@ namespace proxy
protected: protected:
// Implements TCPIPAcceptor // Implements TCPIPAcceptor
std::shared_ptr<i2p::client::I2PServiceHandler> CreateHandler(boost::asio::ip::tcp::socket * socket); std::shared_ptr<i2p::client::I2PServiceHandler> CreateHandler(std::shared_ptr<boost::asio::ip::tcp::socket> socket);
const char* GetName() { return "HTTP Proxy"; } const char* GetName() { return "HTTP Proxy"; }
}; };

13
I2PService.cpp

@ -48,31 +48,30 @@ namespace client
void TCPIPAcceptor::Accept () void TCPIPAcceptor::Accept ()
{ {
auto newSocket = new boost::asio::ip::tcp::socket (GetService ()); auto newSocket = std::make_shared<boost::asio::ip::tcp::socket> (GetService ());
m_Acceptor.async_accept (*newSocket, std::bind (&TCPIPAcceptor::HandleAccept, this, m_Acceptor.async_accept (*newSocket, std::bind (&TCPIPAcceptor::HandleAccept, this,
std::placeholders::_1, newSocket)); std::placeholders::_1, newSocket));
} }
void TCPIPAcceptor::HandleAccept (const boost::system::error_code& ecode, boost::asio::ip::tcp::socket * socket) void TCPIPAcceptor::HandleAccept (const boost::system::error_code& ecode, std::shared_ptr<boost::asio::ip::tcp::socket> socket)
{ {
if (!ecode) if (!ecode)
{ {
LogPrint(eLogDebug,"--- ",GetName()," accepted"); LogPrint(eLogDebug,"--- ",GetName()," accepted");
auto handler = CreateHandler(socket); auto handler = CreateHandler(socket);
if (handler) { if (handler)
{
AddHandler(handler); AddHandler(handler);
handler->Handle(); handler->Handle();
} else {
socket->close();
delete socket;
} }
else
socket->close();
Accept(); Accept();
} }
else else
{ {
if (ecode != boost::asio::error::operation_aborted) if (ecode != boost::asio::error::operation_aborted)
LogPrint (eLogError,"--- ",GetName()," Closing socket on accept because: ", ecode.message ()); LogPrint (eLogError,"--- ",GetName()," Closing socket on accept because: ", ecode.message ());
delete socket;
} }
} }

4
I2PService.h

@ -95,11 +95,11 @@ namespace client
//If you override this make sure you call it from the children //If you override this make sure you call it from the children
void Stop (); void Stop ();
protected: protected:
virtual std::shared_ptr<I2PServiceHandler> CreateHandler(boost::asio::ip::tcp::socket * socket) = 0; virtual std::shared_ptr<I2PServiceHandler> CreateHandler(std::shared_ptr<boost::asio::ip::tcp::socket> socket) = 0;
virtual const char* GetName() { return "Generic TCP/IP accepting daemon"; } virtual const char* GetName() { return "Generic TCP/IP accepting daemon"; }
private: private:
void Accept(); void Accept();
void HandleAccept(const boost::system::error_code& ecode, boost::asio::ip::tcp::socket * socket); void HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<boost::asio::ip::tcp::socket> socket);
boost::asio::ip::tcp::acceptor m_Acceptor; boost::asio::ip::tcp::acceptor m_Acceptor;
boost::asio::deadline_timer m_Timer; boost::asio::deadline_timer m_Timer;
}; };

15
I2PTunnel.cpp

@ -9,7 +9,7 @@ namespace i2p
{ {
namespace client namespace client
{ {
I2PTunnelConnection::I2PTunnelConnection (I2PService * owner, boost::asio::ip::tcp::socket * socket, I2PTunnelConnection::I2PTunnelConnection (I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> socket,
std::shared_ptr<const i2p::data::LeaseSet> leaseSet, int port): std::shared_ptr<const i2p::data::LeaseSet> leaseSet, int port):
I2PServiceHandler(owner), m_Socket (socket), m_RemoteEndpoint (socket->remote_endpoint ()), I2PServiceHandler(owner), m_Socket (socket), m_RemoteEndpoint (socket->remote_endpoint ()),
m_IsQuiet (true) m_IsQuiet (true)
@ -18,14 +18,14 @@ namespace client
} }
I2PTunnelConnection::I2PTunnelConnection (I2PService * owner, I2PTunnelConnection::I2PTunnelConnection (I2PService * owner,
boost::asio::ip::tcp::socket * socket, std::shared_ptr<i2p::stream::Stream> stream): std::shared_ptr<boost::asio::ip::tcp::socket> socket, std::shared_ptr<i2p::stream::Stream> stream):
I2PServiceHandler(owner), m_Socket (socket), m_Stream (stream), I2PServiceHandler(owner), m_Socket (socket), m_Stream (stream),
m_RemoteEndpoint (socket->remote_endpoint ()), m_IsQuiet (true) m_RemoteEndpoint (socket->remote_endpoint ()), m_IsQuiet (true)
{ {
} }
I2PTunnelConnection::I2PTunnelConnection (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream, I2PTunnelConnection::I2PTunnelConnection (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream,
boost::asio::ip::tcp::socket * socket, const boost::asio::ip::tcp::endpoint& target, bool quiet): std::shared_ptr<boost::asio::ip::tcp::socket> socket, const boost::asio::ip::tcp::endpoint& target, bool quiet):
I2PServiceHandler(owner), m_Socket (socket), m_Stream (stream), I2PServiceHandler(owner), m_Socket (socket), m_Stream (stream),
m_RemoteEndpoint (target), m_IsQuiet (quiet) m_RemoteEndpoint (target), m_IsQuiet (quiet)
{ {
@ -155,7 +155,7 @@ namespace client
{ {
public: public:
I2PClientTunnelHandler (I2PClientTunnel * parent, i2p::data::IdentHash destination, I2PClientTunnelHandler (I2PClientTunnel * parent, i2p::data::IdentHash destination,
int destinationPort, boost::asio::ip::tcp::socket * socket): int destinationPort, std::shared_ptr<boost::asio::ip::tcp::socket> socket):
I2PServiceHandler(parent), m_DestinationIdentHash(destination), I2PServiceHandler(parent), m_DestinationIdentHash(destination),
m_DestinationPort (destinationPort), m_Socket(socket) {}; m_DestinationPort (destinationPort), m_Socket(socket) {};
void Handle(); void Handle();
@ -164,7 +164,7 @@ namespace client
void HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream); void HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream);
i2p::data::IdentHash m_DestinationIdentHash; i2p::data::IdentHash m_DestinationIdentHash;
int m_DestinationPort; int m_DestinationPort;
boost::asio::ip::tcp::socket * m_Socket; std::shared_ptr<boost::asio::ip::tcp::socket> m_Socket;
}; };
void I2PClientTunnelHandler::Handle() void I2PClientTunnelHandler::Handle()
@ -198,7 +198,6 @@ namespace client
if (m_Socket) if (m_Socket)
{ {
m_Socket->close(); m_Socket->close();
delete m_Socket;
m_Socket = nullptr; m_Socket = nullptr;
} }
Done(shared_from_this()); Done(shared_from_this());
@ -236,7 +235,7 @@ namespace client
return m_DestinationIdentHash; return m_DestinationIdentHash;
} }
std::shared_ptr<I2PServiceHandler> I2PClientTunnel::CreateHandler(boost::asio::ip::tcp::socket * socket) std::shared_ptr<I2PServiceHandler> I2PClientTunnel::CreateHandler(std::shared_ptr<boost::asio::ip::tcp::socket> socket)
{ {
const i2p::data::IdentHash *identHash = GetIdentHash(); const i2p::data::IdentHash *identHash = GetIdentHash();
if (identHash) if (identHash)
@ -296,7 +295,7 @@ namespace client
return; return;
} }
} }
auto conn = std::make_shared<I2PTunnelConnection> (this, stream, new boost::asio::ip::tcp::socket (GetService ()), m_Endpoint); auto conn = std::make_shared<I2PTunnelConnection> (this, stream, std::make_shared<boost::asio::ip::tcp::socket> (GetService ()), m_Endpoint);
AddHandler (conn); AddHandler (conn);
conn->Connect (); conn->Connect ();
} }

10
I2PTunnel.h

@ -24,11 +24,11 @@ namespace client
{ {
public: public:
I2PTunnelConnection (I2PService * owner, boost::asio::ip::tcp::socket * socket, I2PTunnelConnection (I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> socket,
std::shared_ptr<const i2p::data::LeaseSet> leaseSet, int port = 0); // to I2P std::shared_ptr<const i2p::data::LeaseSet> leaseSet, int port = 0); // to I2P
I2PTunnelConnection (I2PService * owner, boost::asio::ip::tcp::socket * socket, I2PTunnelConnection (I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> socket,
std::shared_ptr<i2p::stream::Stream> stream); // to I2P using simplified API :) std::shared_ptr<i2p::stream::Stream> stream); // to I2P using simplified API :)
I2PTunnelConnection (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream, boost::asio::ip::tcp::socket * socket, I2PTunnelConnection (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream, std::shared_ptr<boost::asio::ip::tcp::socket> socket,
const boost::asio::ip::tcp::endpoint& target, bool quiet = true); // from I2P const boost::asio::ip::tcp::endpoint& target, bool quiet = true); // from I2P
~I2PTunnelConnection (); ~I2PTunnelConnection ();
void I2PConnect (const uint8_t * msg = nullptr, size_t len = 0); void I2PConnect (const uint8_t * msg = nullptr, size_t len = 0);
@ -49,7 +49,7 @@ namespace client
private: private:
uint8_t m_Buffer[I2P_TUNNEL_CONNECTION_BUFFER_SIZE], m_StreamBuffer[I2P_TUNNEL_CONNECTION_BUFFER_SIZE]; uint8_t m_Buffer[I2P_TUNNEL_CONNECTION_BUFFER_SIZE], m_StreamBuffer[I2P_TUNNEL_CONNECTION_BUFFER_SIZE];
std::unique_ptr<boost::asio::ip::tcp::socket> m_Socket; std::shared_ptr<boost::asio::ip::tcp::socket> m_Socket;
std::shared_ptr<i2p::stream::Stream> m_Stream; std::shared_ptr<i2p::stream::Stream> m_Stream;
boost::asio::ip::tcp::endpoint m_RemoteEndpoint; boost::asio::ip::tcp::endpoint m_RemoteEndpoint;
bool m_IsQuiet; // don't send destination bool m_IsQuiet; // don't send destination
@ -60,7 +60,7 @@ namespace client
protected: protected:
// Implements TCPIPAcceptor // Implements TCPIPAcceptor
std::shared_ptr<I2PServiceHandler> CreateHandler(boost::asio::ip::tcp::socket * socket); std::shared_ptr<I2PServiceHandler> CreateHandler(std::shared_ptr<boost::asio::ip::tcp::socket> socket);
const char* GetName() { return "I2P Client Tunnel"; } const char* GetName() { return "I2P Client Tunnel"; }
public: public:

7
SOCKS.cpp

@ -118,7 +118,7 @@ namespace proxy
void HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream); void HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream);
uint8_t m_sock_buff[socks_buffer_size]; uint8_t m_sock_buff[socks_buffer_size];
boost::asio::ip::tcp::socket * m_sock; std::shared_ptr<boost::asio::ip::tcp::socket> m_sock;
std::shared_ptr<i2p::stream::Stream> m_stream; std::shared_ptr<i2p::stream::Stream> m_stream;
uint8_t *m_remaining_data; //Data left to be sent uint8_t *m_remaining_data; //Data left to be sent
uint8_t m_response[7+max_socks_hostname_size]; uint8_t m_response[7+max_socks_hostname_size];
@ -135,7 +135,7 @@ namespace proxy
state m_state; state m_state;
public: public:
SOCKSHandler(SOCKSServer * parent, boost::asio::ip::tcp::socket * sock) : SOCKSHandler(SOCKSServer * parent, std::shared_ptr<boost::asio::ip::tcp::socket> sock) :
I2PServiceHandler(parent), m_sock(sock), m_stream(nullptr), I2PServiceHandler(parent), m_sock(sock), m_stream(nullptr),
m_authchosen(AUTH_UNACCEPTABLE), m_addrtype(ADDR_IPV4) m_authchosen(AUTH_UNACCEPTABLE), m_addrtype(ADDR_IPV4)
{ m_address.ip = 0; EnterState(GET_SOCKSV); } { m_address.ip = 0; EnterState(GET_SOCKSV); }
@ -161,7 +161,6 @@ namespace proxy
{ {
LogPrint(eLogDebug,"--- SOCKS close sock"); LogPrint(eLogDebug,"--- SOCKS close sock");
m_sock->close(); m_sock->close();
delete m_sock;
m_sock = nullptr; m_sock = nullptr;
} }
if (m_stream) if (m_stream)
@ -566,7 +565,7 @@ namespace proxy
{ {
} }
std::shared_ptr<i2p::client::I2PServiceHandler> SOCKSServer::CreateHandler(boost::asio::ip::tcp::socket * socket) std::shared_ptr<i2p::client::I2PServiceHandler> SOCKSServer::CreateHandler(std::shared_ptr<boost::asio::ip::tcp::socket> socket)
{ {
return std::make_shared<SOCKSHandler> (this, socket); return std::make_shared<SOCKSHandler> (this, socket);
} }

2
SOCKS.h

@ -20,7 +20,7 @@ namespace proxy
protected: protected:
// Implements TCPIPAcceptor // Implements TCPIPAcceptor
std::shared_ptr<i2p::client::I2PServiceHandler> CreateHandler(boost::asio::ip::tcp::socket * socket); std::shared_ptr<i2p::client::I2PServiceHandler> CreateHandler(std::shared_ptr<boost::asio::ip::tcp::socket> socket);
const char* GetName() { return "SOCKS"; } const char* GetName() { return "SOCKS"; }
}; };

Loading…
Cancel
Save