Socket with auto-detect SSL on inbound, SSL and non-SSL outbound.

Compiles, but totally untested.
 #	../websocketpp/src/sockets/tls-hybrid.hpp
This commit is contained in:
JoelKatz
2013-01-23 13:35:55 -08:00
parent e25863207a
commit 6ae38db4ed
2 changed files with 122 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
#include "AutoSocket.h"
#include <boost/bind.hpp>
void AutoSocket::handle_autodetect(const error_code& ec)
{
if (ec)
{
if (mCallback)
mCallback(ec);
return;
}
if ((mBuffer[0] < 127) && (mBuffer[0] > 31) &&
(mBuffer[1] < 127) && (mBuffer[1] > 31) &&
(mBuffer[2] < 127) && (mBuffer[2] > 31) &&
(mBuffer[3] < 127) && (mBuffer[3] > 31))
{ // non-SSL
if (mCallback)
mCallback(ec);
}
else
{ // ssl
mSecure = true;
SSLSocket().async_handshake(ssl_socket::server, mCallback);
}
}

View File

@@ -0,0 +1,94 @@
#ifndef __AUTOSOCKET_H_
#define __AUTOSOCKET_H_
#include <vector>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
// Socket wrapper that supports both SSL and non-SSL connections.
// Generally, handle it as you would an SSL connection.
// For outbound non-SSL connections, just don't call async_handshake.
namespace basio = boost::asio;
namespace bassl = basio::ssl;
class AutoSocket
{
public:
typedef bassl::stream<basio::ip::tcp::socket> ssl_socket;
typedef ssl_socket::next_layer_type plain_socket;
typedef boost::system::error_code error_code;
typedef boost::function<void(error_code)> callback;
protected:
ssl_socket mSocket;
bool mSecure;
callback mCallback;
std::vector<char> mBuffer;
public:
AutoSocket(basio::io_service& s, bassl::context& c) : mSocket(s, c), mSecure(false), mBuffer(4) { ; }
bool isSecure() { return mSecure; }
ssl_socket& SSLSocket() { return mSocket; }
plain_socket& PlainSocket() { return mSocket.next_layer(); }
void async_handshake(ssl_socket::handshake_type type, callback cbFunc)
{
mSecure = true;
if (type == ssl_socket::client)
SSLSocket().async_handshake(type, cbFunc);
else
{
mCallback = cbFunc;
PlainSocket().async_receive(basio::buffer(mBuffer), basio::socket_base::message_peek,
boost::bind(&AutoSocket::handle_autodetect, this, basio::placeholders::error));
}
}
template <typename StreamType> StreamType& getSocket()
{
if (isSecure())
return SSLSocket();
if (!isSecure())
return PlainSocket();
}
template <typename ShutdownHandler> void async_shutdown(ShutdownHandler handler)
{
if (isSecure())
SSLSocket().async_shutdown(handler);
else
{
PlainSocket().shutdown(plain_socket::shutdown_both);
if (handler)
mSocket.get_io_service().post(handler);
}
}
template <typename Seq, typename Handler> void async_read_some(const Seq& buffers, Handler handler)
{
if (isSecure())
SSLSocket().async_read_some(buffers, handler);
else
PlainSocket().async_read_some(buffers, handler);
}
template <typename Seq, typename Handler> void async_write_some(const Seq& buffers, Handler handler)
{
if (isSecure())
SSLSocket().async_write_some(buffers, handler);
else
PlainSocket().async_write_some(buffers, handler);
}
protected:
void handle_autodetect(const error_code&);
};
#endif