]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp03/ssl/server.cpp
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
13 #include <boost/bind.hpp>
14 #include <boost/asio.hpp>
15 #include <boost/asio/ssl.hpp>
17 typedef boost::asio::ssl::stream
<boost::asio::ip::tcp::socket
> ssl_socket
;
22 session(boost::asio::io_context
& io_context
,
23 boost::asio::ssl::context
& context
)
24 : socket_(io_context
, context
)
28 ssl_socket::lowest_layer_type
& socket()
30 return socket_
.lowest_layer();
35 socket_
.async_handshake(boost::asio::ssl::stream_base::server
,
36 boost::bind(&session::handle_handshake
, this,
37 boost::asio::placeholders::error
));
40 void handle_handshake(const boost::system::error_code
& error
)
44 socket_
.async_read_some(boost::asio::buffer(data_
, max_length
),
45 boost::bind(&session::handle_read
, this,
46 boost::asio::placeholders::error
,
47 boost::asio::placeholders::bytes_transferred
));
55 void handle_read(const boost::system::error_code
& error
,
56 size_t bytes_transferred
)
60 boost::asio::async_write(socket_
,
61 boost::asio::buffer(data_
, bytes_transferred
),
62 boost::bind(&session::handle_write
, this,
63 boost::asio::placeholders::error
));
71 void handle_write(const boost::system::error_code
& error
)
75 socket_
.async_read_some(boost::asio::buffer(data_
, max_length
),
76 boost::bind(&session::handle_read
, this,
77 boost::asio::placeholders::error
,
78 boost::asio::placeholders::bytes_transferred
));
88 enum { max_length
= 1024 };
89 char data_
[max_length
];
95 server(boost::asio::io_context
& io_context
, unsigned short port
)
96 : io_context_(io_context
),
98 boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port
)),
99 context_(boost::asio::ssl::context::sslv23
)
101 context_
.set_options(
102 boost::asio::ssl::context::default_workarounds
103 | boost::asio::ssl::context::no_sslv2
104 | boost::asio::ssl::context::single_dh_use
);
105 context_
.set_password_callback(boost::bind(&server::get_password
, this));
106 context_
.use_certificate_chain_file("server.pem");
107 context_
.use_private_key_file("server.pem", boost::asio::ssl::context::pem
);
108 context_
.use_tmp_dh_file("dh2048.pem");
113 std::string
get_password() const
120 session
* new_session
= new session(io_context_
, context_
);
121 acceptor_
.async_accept(new_session
->socket(),
122 boost::bind(&server::handle_accept
, this, new_session
,
123 boost::asio::placeholders::error
));
126 void handle_accept(session
* new_session
,
127 const boost::system::error_code
& error
)
131 new_session
->start();
142 boost::asio::io_context
& io_context_
;
143 boost::asio::ip::tcp::acceptor acceptor_
;
144 boost::asio::ssl::context context_
;
147 int main(int argc
, char* argv
[])
153 std::cerr
<< "Usage: server <port>\n";
157 boost::asio::io_context io_context
;
159 using namespace std
; // For atoi.
160 server
s(io_context
, atoi(argv
[1]));
164 catch (std::exception
& e
)
166 std::cerr
<< "Exception: " << e
.what() << "\n";