-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.cpp
executable file
·140 lines (125 loc) · 4.84 KB
/
client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* tls-example
* Copyright (c) 2020-2023 Peter Nebe <mail@peter-nebe.dev>
*
* This file is part of tls-example.
*
* tls-example is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* tls-example is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with tls-example. If not, see <https://www.gnu.org/licenses/>.
*/
#include "boost/asio.hpp"
#include "boost/asio/ssl.hpp"
#include <iostream>
#include <string>
using boost::asio::ip::tcp;
using namespace boost::asio;
using namespace std;
const size_t maxLength = 1000;
class SslContext : public ssl::context
{
public:
SslContext()
: ssl::context(ssl::context::tls_client)
{
set_password_callback([](size_t, ssl::context::password_purpose){ return "tls-example"s; });
use_private_key_file("tls-example-client-1.key", ssl::context::pem);
use_certificate_file("tls-example-client-1.crt", ssl::context::pem);
load_verify_file("tls-example-ca.crt");
set_verify_mode(ssl::verify_peer | ssl::verify_fail_if_no_peer_cert);
set_verify_callback(ssl::host_name_verification("tls-example-server"));
}
}; // class SslContext
class Client
{
public:
Client(io_context &ioctx, const char *server, const char *port)
: socket(ioctx, sslctx),
readbuf(maxLength, '\0')
{
connect(tcp::resolver(ioctx).resolve(server, port));
}
private:
void connect(const tcp::resolver::results_type &endpoints)
{
async_connect(socket.lowest_layer(), endpoints, [this](const boost::system::error_code &error, const tcp::endpoint&)
{
if(error)
cout << "Connect failed: " << error.message() << endl;
else
handshake();
});
}
void handshake()
{
socket.async_handshake(ssl::stream_base::client, [this](const boost::system::error_code &error)
{
if(error)
cout << "Handshake failed: " << error.message() << endl;
else
sendRequest();
});
}
void sendRequest()
{
cout << "Enter phrase: ";
getline(cin, writebuf);
async_write(socket, buffer(writebuf, maxLength), [this](const boost::system::error_code &error, size_t length)
{
if(error)
cout << "Write failed: " << error.message() << endl;
else
receiveResponse(length);
});
}
void receiveResponse(size_t length)
{
async_read(socket, buffer(readbuf, length), [this](const boost::system::error_code &error, size_t length)
{
if(error)
{
cout << "Read failed: " << error.message() << endl;
}
else
{
cout << "Response: " << readbuf.substr(0, length) << endl;
sendRequest();
}
});
}
SslContext sslctx;
ssl::stream<tcp::socket> socket;
string writebuf;
string readbuf;
}; // class Client
int main(int argc, char *argv[])
{
try
{
if(argc != 3)
{
cerr << "Usage: " << argv[0] << " SERVER PORT" << endl;
return 1;
}
const char *server = argv[1];
const char *port = argv[2];
io_context ioctx;
Client clnt(ioctx, server, port);
ioctx.run();
}
catch(const exception &ex)
{
cerr << "Exception: " << ex.what() << endl;
return 1;
}
return 0;
}