aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlloyd <[email protected]>2012-01-27 00:48:23 +0000
committerlloyd <[email protected]>2012-01-27 00:48:23 +0000
commit3d3c7f45e64c2dab1b9558fc9da80cdd30175063 (patch)
tree678b373878366651106baa5e716cd6ecde5a6e97
parent25b549117c2f0c1e01810c9929a4204e846b70b8 (diff)
Use boost::asio instead of standalone asio as it's more commonly
available. If an error occurs in loading a certificate, etc then simply ignore it.
-rw-r--r--doc/examples/GNUmakefile2
-rw-r--r--doc/examples/asio_tls_server.cpp49
-rw-r--r--doc/examples/credentials.h42
3 files changed, 49 insertions, 44 deletions
diff --git a/doc/examples/GNUmakefile b/doc/examples/GNUmakefile
index a27ad25a9..23045961e 100644
--- a/doc/examples/GNUmakefile
+++ b/doc/examples/GNUmakefile
@@ -21,4 +21,4 @@ eax_test: eax_test.cpp
$(CXX) $(CFLAGS) $? $(LIBS) -lboost_regex -o $@
asio_tls_server: asio_tls_server.cpp
- $(CXX) $(CFLAGS) $? $(LIBS) -lboost_thread -o $@
+ $(CXX) $(CFLAGS) $? $(LIBS) -lboost_thread -lboost_system -o $@
diff --git a/doc/examples/asio_tls_server.cpp b/doc/examples/asio_tls_server.cpp
index 58f59d968..fff83f5f8 100644
--- a/doc/examples/asio_tls_server.cpp
+++ b/doc/examples/asio_tls_server.cpp
@@ -2,13 +2,12 @@
#include <string>
#include <vector>
+#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
-#include <asio.hpp>
-
#include <botan/tls_server.h>
#include <botan/x509cert.h>
#include <botan/pkcs8.h>
@@ -18,14 +17,14 @@
#include "credentials.h"
using Botan::byte;
-using asio::ip::tcp;
+using boost::asio::ip::tcp;
class tls_server_session : public boost::enable_shared_from_this<tls_server_session>
{
public:
typedef boost::shared_ptr<tls_server_session> pointer;
- static pointer create(asio::io_service& io_service,
+ static pointer create(boost::asio::io_service& io_service,
Botan::TLS::Session_Manager& session_manager,
Botan::Credentials_Manager& credentials,
Botan::TLS::Policy& policy,
@@ -46,17 +45,17 @@ class tls_server_session : public boost::enable_shared_from_this<tls_server_sess
void start()
{
m_socket.async_read_some(
- asio::buffer(m_read_buf, sizeof(m_read_buf)),
+ boost::asio::buffer(m_read_buf, sizeof(m_read_buf)),
m_strand.wrap(
boost::bind(&tls_server_session::handle_read, shared_from_this(),
- asio::placeholders::error,
- asio::placeholders::bytes_transferred)));
+ boost::asio::placeholders::error,
+ boost::asio::placeholders::bytes_transferred)));
}
void stop() { m_socket.close(); }
private:
- tls_server_session(asio::io_service& io_service,
+ tls_server_session(boost::asio::io_service& io_service,
Botan::TLS::Session_Manager& session_manager,
Botan::Credentials_Manager& credentials,
Botan::TLS::Policy& policy,
@@ -73,7 +72,7 @@ class tls_server_session : public boost::enable_shared_from_this<tls_server_sess
{
}
- void handle_read(const asio::error_code& error,
+ void handle_read(const boost::system::error_code& error,
size_t bytes_transferred)
{
if(!error)
@@ -90,10 +89,10 @@ class tls_server_session : public boost::enable_shared_from_this<tls_server_sess
}
m_socket.async_read_some(
- asio::buffer(m_read_buf, sizeof(m_read_buf)),
+ boost::asio::buffer(m_read_buf, sizeof(m_read_buf)),
m_strand.wrap(boost::bind(&tls_server_session::handle_read, shared_from_this(),
- asio::placeholders::error,
- asio::placeholders::bytes_transferred)));
+ boost::asio::placeholders::error,
+ boost::asio::placeholders::bytes_transferred)));
}
else
{
@@ -101,8 +100,7 @@ class tls_server_session : public boost::enable_shared_from_this<tls_server_sess
}
}
- void handle_write(const asio::error_code& error,
- size_t bytes_transferred)
+ void handle_write(const boost::system::error_code& error)
{
if(!error)
{
@@ -127,13 +125,12 @@ class tls_server_session : public boost::enable_shared_from_this<tls_server_sess
{
std::swap(m_outbox, m_write_buf);
- asio::async_write(m_socket,
- asio::buffer(&m_write_buf[0], m_write_buf.size()),
+ boost::asio::async_write(m_socket,
+ boost::asio::buffer(&m_write_buf[0], m_write_buf.size()),
m_strand.wrap(
boost::bind(&tls_server_session::handle_write,
shared_from_this(),
- asio::placeholders::error,
- asio::placeholders::bytes_transferred)));
+ boost::asio::placeholders::error)));
}
}
@@ -174,7 +171,7 @@ class tls_server_session : public boost::enable_shared_from_this<tls_server_sess
return true;
}
- asio::io_service::strand m_strand; // serialization
+ boost::asio::io_service::strand m_strand; // serialization
tcp::socket m_socket;
Botan::TLS::Server m_tls;
@@ -229,7 +226,7 @@ class tls_server
public:
typedef tls_server_session session;
- tls_server(asio::io_service& io_service, unsigned short port) :
+ tls_server(boost::asio::io_service& io_service, unsigned short port) :
m_acceptor(io_service, tcp::endpoint(tcp::v4(), port)),
m_creds(m_rng)
{
@@ -241,7 +238,7 @@ class tls_server
&tls_server::handle_accept,
this,
new_session,
- asio::placeholders::error)
+ boost::asio::placeholders::error)
);
}
@@ -249,7 +246,7 @@ class tls_server
session::pointer make_session()
{
return session::create(
- m_acceptor.io_service(),
+ m_acceptor.get_io_service(),
m_session_manager,
m_creds,
m_policy,
@@ -258,7 +255,7 @@ class tls_server
}
void handle_accept(session::pointer new_session,
- const asio::error_code& error)
+ const boost::system::error_code& error)
{
if (!error)
{
@@ -272,7 +269,7 @@ class tls_server
&tls_server::handle_accept,
this,
new_session,
- asio::placeholders::error)
+ boost::asio::placeholders::error)
);
}
}
@@ -290,7 +287,7 @@ int main()
try
{
Botan::LibraryInitializer init("thread_safe=true");
- asio::io_service io_service;
+ boost::asio::io_service io_service;
unsigned short port = 4433;
tls_server server(io_service, port);
@@ -308,7 +305,7 @@ int main()
{
boost::shared_ptr<boost::thread> thread(
new boost::thread(
- boost::bind(&asio::io_service::run, &io_service)));
+ boost::bind(&boost::asio::io_service::run, &io_service)));
threads.push_back(thread);
}
diff --git a/doc/examples/credentials.h b/doc/examples/credentials.h
index 3d70a092d..e97d28e5d 100644
--- a/doc/examples/credentials.h
+++ b/doc/examples/credentials.h
@@ -3,6 +3,7 @@
#define EXAMPLE_CREDENTIALS_MANAGER_H__
#include <botan/credentials_manager.h>
+#include <iostream>
bool value_exists(const std::vector<std::string>& vec,
const std::string& val)
@@ -25,34 +26,41 @@ class Credentials_Manager_Simple : public Botan::Credentials_Manager
{
std::vector<Botan::X509_Certificate> certs;
- if(type == "tls-server")
+ try
{
- const std::string hostname = (context == "" ? "localhost" : context);
-
- if(value_exists(cert_key_types, "RSA"))
+ if(type == "tls-server")
{
- Botan::X509_Certificate cert(hostname + ".crt");
- Botan::Private_Key* key = Botan::PKCS8::load_key(hostname + ".key", rng);
+ const std::string hostname = (context == "" ? "localhost" : context);
- certs_and_keys[cert] = key;
- certs.push_back(cert);
+ if(value_exists(cert_key_types, "RSA"))
+ {
+ Botan::X509_Certificate cert(hostname + ".crt");
+ Botan::Private_Key* key = Botan::PKCS8::load_key(hostname + ".key", rng);
+
+ certs_and_keys[cert] = key;
+ certs.push_back(cert);
+ }
+ else if(value_exists(cert_key_types, "DSA"))
+ {
+ Botan::X509_Certificate cert(hostname + ".dsa.crt");
+ Botan::Private_Key* key = Botan::PKCS8::load_key(hostname + ".dsa.key", rng);
+
+ certs_and_keys[cert] = key;
+ certs.push_back(cert);
+ }
}
- else if(value_exists(cert_key_types, "DSA"))
+ else if(type == "tls-client")
{
- Botan::X509_Certificate cert(hostname + ".dsa.crt");
- Botan::Private_Key* key = Botan::PKCS8::load_key(hostname + ".dsa.key", rng);
+ Botan::X509_Certificate cert("user-rsa.crt");
+ Botan::Private_Key* key = Botan::PKCS8::load_key("user-rsa.key", rng);
certs_and_keys[cert] = key;
certs.push_back(cert);
}
}
- else if(type == "tls-client")
+ catch(std::exception& e)
{
- Botan::X509_Certificate cert("user-rsa.crt");
- Botan::Private_Key* key = Botan::PKCS8::load_key("user-rsa.key", rng);
-
- certs_and_keys[cert] = key;
- certs.push_back(cert);
+ std::cout << e.what() << "\n";
}
return certs;