|
| 1 | +// tlsecho.cpp |
| 2 | +// |
| 3 | +// A TLS echo client that demonstrates session resumption. |
| 4 | +// |
| 5 | +// USAGE: |
| 6 | +// tlsecho [[host] port [ca.pem]] |
| 7 | +// |
| 8 | +// The host defaults to "localhost". If the first argument is a bare |
| 9 | +// integer it is taken as the port number (host keeps its default), so |
| 10 | +// both of the following work: |
| 11 | +// |
| 12 | +// tlsecho 4433 ca.pem |
| 13 | +// tlsecho myserver 4433 ca.pem |
| 14 | +// |
| 15 | +// Reads lines from stdin and sends them to the echo server, printing the |
| 16 | +// echoed response. An empty line closes the connection. |
| 17 | +// |
| 18 | +// When built with OpenSSL, the client saves the session from the first |
| 19 | +// connection and offers it on the second, demonstrating TLS session |
| 20 | +// resumption. The handshake type (full or resumed) is printed for every |
| 21 | +// connection so the speed-up is easy to observe. |
| 22 | +// |
| 23 | +// -------------------------------------------------------------------------- |
| 24 | +// This file is part of the "sockpp" C++ socket library. |
| 25 | +// |
| 26 | +// Copyright (c) 2026 Frank Pagliughi |
| 27 | +// All rights reserved. |
| 28 | +// |
| 29 | +// Redistribution and use in source and binary forms, with or without |
| 30 | +// modification, are permitted provided that the following conditions are |
| 31 | +// met: |
| 32 | +// |
| 33 | +// 1. Redistributions of source code must retain the above copyright notice, |
| 34 | +// this list of conditions and the following disclaimer. |
| 35 | +// |
| 36 | +// 2. Redistributions in binary form must reproduce the above copyright |
| 37 | +// notice, this list of conditions and the following disclaimer in the |
| 38 | +// documentation and/or other materials provided with the distribution. |
| 39 | +// |
| 40 | +// 3. Neither the name of the copyright holder nor the names of its |
| 41 | +// contributors may be used to endorse or promote products derived from this |
| 42 | +// software without specific prior written permission. |
| 43 | +// |
| 44 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 45 | +// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 46 | +// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 47 | +// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 48 | +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 49 | +// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 50 | +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 51 | +// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 52 | +// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 53 | +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 54 | +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 55 | +// -------------------------------------------------------------------------- |
| 56 | + |
| 57 | +#include <cstring> |
| 58 | +#include <iostream> |
| 59 | +#include <string> |
| 60 | +#include <system_error> |
| 61 | + |
| 62 | +#include "sockpp/inet_address.h" |
| 63 | +#include "sockpp/tls/connector.h" |
| 64 | +#include "sockpp/tls/context.h" |
| 65 | +#include "sockpp/version.h" |
| 66 | + |
| 67 | +#if defined(SOCKPP_OPENSSL) |
| 68 | + #include "sockpp/tls/openssl_session.h" |
| 69 | +#endif |
| 70 | + |
| 71 | +using namespace std; |
| 72 | + |
| 73 | +// Runs the echo loop on an already-connected TLS socket. |
| 74 | +// Returns when the user types an empty line or stdin reaches EOF. |
| 75 | +static bool run_echo_loop(sockpp::tls_connector& conn) { |
| 76 | + string s, sret; |
| 77 | + |
| 78 | + while (getline(cin, s) && !s.empty()) { |
| 79 | + if (auto res = conn.write(s); !res) { |
| 80 | + cerr << "Write error: " << res.error_message() << "\n"; |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + sret.resize(s.size()); |
| 85 | + if (auto res = conn.read_n(sret.data(), s.size()); !res || res.value() != s.size()) { |
| 86 | + cerr << "Read error: " << (!res ? res.error_message() : "connection closed") |
| 87 | + << "\n"; |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + cout << sret << "\n"; |
| 92 | + } |
| 93 | + |
| 94 | + return true; |
| 95 | +} |
| 96 | + |
| 97 | +// -------------------------------------------------------------------------- |
| 98 | + |
| 99 | +int main(int argc, char* argv[]) { |
| 100 | + cout << "TLS echo client for 'sockpp' " << sockpp::SOCKPP_VERSION << "\n" << endl; |
| 101 | + |
| 102 | + // Argument parsing: if argv[1] is a bare integer treat it as the port |
| 103 | + // (host defaults to "localhost"), otherwise argv[1] is the host. |
| 104 | + // This lets both "tlsecho 4433 ca.pem" and "tlsecho myhost 4433 ca.pem" work. |
| 105 | + string host = "localhost"; |
| 106 | + in_port_t port = 4433; |
| 107 | + string ca_file; |
| 108 | + |
| 109 | + int next = 1; |
| 110 | + if (argc > next) { |
| 111 | + const char* a = argv[next]; |
| 112 | + // Purely numeric first arg → port number, host stays "localhost" |
| 113 | + bool is_port = (*a != '\0' && strspn(a, "0123456789") == strlen(a)); |
| 114 | + if (is_port) { |
| 115 | + port = static_cast<in_port_t>(atoi(a)); |
| 116 | + } |
| 117 | + else { |
| 118 | + host = a; |
| 119 | + ++next; |
| 120 | + if (argc > next) |
| 121 | + port = static_cast<in_port_t>(atoi(argv[next])); |
| 122 | + } |
| 123 | + ++next; |
| 124 | + } |
| 125 | + if (argc > next) |
| 126 | + ca_file = argv[next]; |
| 127 | + |
| 128 | + sockpp::initialize(); |
| 129 | + |
| 130 | + // Build a client context. |
| 131 | + auto ctx = sockpp::tls_context::client(); |
| 132 | + if (ca_file.empty()) |
| 133 | + ctx.set_default_trust_locations(); |
| 134 | + else if (auto res = ctx.set_trust_file(ca_file); !res) { |
| 135 | + cerr << "Failed to load CA file: " << res.error_message() << endl; |
| 136 | + return 1; |
| 137 | + } |
| 138 | + |
| 139 | + error_code ec; |
| 140 | + sockpp::inet_address addr{host, port, ec}; |
| 141 | + if (ec) { |
| 142 | + cerr << "Error resolving '" << host << "': " << ec.message() << endl; |
| 143 | + return 1; |
| 144 | + } |
| 145 | + |
| 146 | + // ---- First connection ---- |
| 147 | + |
| 148 | + sockpp::tls_connector conn{ctx, ec}; |
| 149 | + if (ec) { |
| 150 | + cerr << "Error creating connector: " << ec.message() << endl; |
| 151 | + return 1; |
| 152 | + } |
| 153 | + |
| 154 | + if (auto res = conn.connect(addr); !res) { |
| 155 | + cerr << "Error connecting to " << addr << ": " << res.error_message() << endl; |
| 156 | + return 1; |
| 157 | + } |
| 158 | + |
| 159 | + cout << "Connected to " << addr << " [" << conn.negotiated_version() << "]" |
| 160 | + << " — full handshake" << endl; |
| 161 | + |
| 162 | + run_echo_loop(conn); |
| 163 | + |
| 164 | +#if defined(SOCKPP_OPENSSL) |
| 165 | + cout << "\nAttempting reconnect..." << endl; |
| 166 | + // Save the session before the connector goes out of scope. |
| 167 | + // The close_notify sent by the destructor marks the session as |
| 168 | + // resumable on the server side. |
| 169 | + auto sess_res = conn.get_session(); |
| 170 | + if (!sess_res) { |
| 171 | + cerr << "Warning: could not capture session: " << sess_res.error_message() << endl; |
| 172 | + return 0; |
| 173 | + } |
| 174 | + sockpp::tls_session saved = std::move(sess_res.value()); |
| 175 | + |
| 176 | + // Close the first connection explicitly so the destructor has already |
| 177 | + // run (and sent close_notify) before we attempt the second connect. |
| 178 | + conn = sockpp::tls_connector{ctx, ec}; |
| 179 | + if (ec) { |
| 180 | + cerr << "Error resetting connector: " << ec.message() << endl; |
| 181 | + return 1; |
| 182 | + } |
| 183 | + |
| 184 | + // ---- Second connection: offer the saved session ---- |
| 185 | + |
| 186 | + if (auto res = conn.set_session(saved); !res) { |
| 187 | + cerr << "Warning: set_session failed: " << res.error_message() << endl; |
| 188 | + } |
| 189 | + |
| 190 | + if (auto res = conn.connect(addr); !res) { |
| 191 | + cerr << "Error on second connect: " << res.error_message() << endl; |
| 192 | + return 1; |
| 193 | + } |
| 194 | + |
| 195 | + bool reused = conn.session_reused(); |
| 196 | + cout << "\nReconnected to " << addr << " [" << conn.negotiated_version() << "]" |
| 197 | + << " — " << (reused ? "session RESUMED" : "full handshake (not resumed)") << endl; |
| 198 | + |
| 199 | + run_echo_loop(conn); |
| 200 | +#endif |
| 201 | + |
| 202 | + return 0; |
| 203 | +} |
0 commit comments