2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
42#ifndef APPS_ECHO_MODEL_CLIENT_H
43#define APPS_ECHO_MODEL_CLIENT_H
45#include "log/Logger.h"
47#define QUOTE_INCLUDE(a) STR(a)
51#define SOCKETCLIENT_INCLUDE QUOTE_INCLUDE(net/NET/stream/STREAM/SocketClient.h)
54#include SOCKETCLIENT_INCLUDE
58#ifndef DOXYGEN_SHOULD_SKIP_THIS
62#if (STREAM_TYPE == TLS)
64#include <openssl/ssl.h>
65#include <openssl/x509v3.h>
70#if (STREAM_TYPE == LEGACY)
77 return EchoSocketClient(
"echoclient");
82#elif (STREAM_TYPE == TLS)
84namespace apps::echo::model::tls {
86 using EchoSocketClient = net::NET::stream::tls::SocketClient<EchoClientSocketContextFactory>;
87 using SocketConnection = EchoSocketClient::SocketConnection;
89 EchoSocketClient getClient() {
90 EchoSocketClient client(
"echoclient");
92 client.setOnConnect([&client](SocketConnection* socketConnection) {
93 VLOG(1) <<
"OnConnect " << client.getConfig().getInstanceName();
95 VLOG(1) <<
"\tLocal: " << socketConnection->getLocalAddress().toString();
96 VLOG(1) <<
"\tPeer: " << socketConnection->getRemoteAddress().toString();
108 client.setOnConnected([&client](SocketConnection* socketConnection) {
109 VLOG(1) <<
"OnConnected " << client.getConfig().getInstanceName();
111 X509* server_cert = SSL_get_peer_certificate(socketConnection->getSSL());
112 if (server_cert !=
nullptr) {
113 long verifyErr = SSL_get_verify_result(socketConnection->getSSL());
115 VLOG(1) <<
"\tPeer certificate verifyErr = " + std::to_string(verifyErr) +
": " +
116 std::string(X509_verify_cert_error_string(verifyErr));
118 char* str = X509_NAME_oneline(X509_get_subject_name(server_cert),
nullptr, 0);
119 VLOG(1) <<
"\t Subject: " + std::string(str);
122 str = X509_NAME_oneline(X509_get_issuer_name(server_cert),
nullptr, 0);
123 VLOG(1) <<
"\t Issuer: " + std::string(str);
128 GENERAL_NAMES* subjectAltNames =
129 static_cast<GENERAL_NAMES*>(X509_get_ext_d2i(server_cert, NID_subject_alt_name,
nullptr,
nullptr));
131#pragma GCC diagnostic push
133#if __has_warning("-Wused-but-marked-unused")
134#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
138 int32_t altNameCount = sk_GENERAL_NAME_num(subjectAltNames);
140#pragma GCC diagnostic pop
142 VLOG(1) <<
"\t Subject alternative name count: " << altNameCount;
143 for (int32_t i = 0; i < altNameCount; ++i) {
145#pragma GCC diagnostic push
147#if __has_warning("-Wused-but-marked-unused")
148#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
152 GENERAL_NAME* generalName = sk_GENERAL_NAME_value(subjectAltNames, i);
154#pragma GCC diagnostic pop
156 if (generalName->type == GEN_URI) {
157 std::string subjectAltName =
158 std::string(
reinterpret_cast<
const char*>(ASN1_STRING_get0_data(generalName->d.uniformResourceIdentifier)),
159 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.uniformResourceIdentifier)));
160 VLOG(1) <<
"\t SAN (URI): '" + subjectAltName;
161 }
else if (generalName->type == GEN_DNS) {
162 std::string subjectAltName =
163 std::string(
reinterpret_cast<
const char*>(ASN1_STRING_get0_data(generalName->d.dNSName)),
164 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.dNSName)));
165 VLOG(1) <<
"\t SAN (DNS): '" + subjectAltName;
167 VLOG(1) <<
"\t SAN (Type): '" + std::to_string(generalName->type);
171#pragma GCC diagnostic push
173#if __has_warning("-Wused-but-marked-unused")
174#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
178 sk_GENERAL_NAME_pop_free(subjectAltNames, GENERAL_NAME_free);
180#pragma GCC diagnostic pop
182 X509_free(server_cert);
184 VLOG(1) <<
"\tPeer certificate: no certificate";
188 client.setOnDisconnect([&client](SocketConnection* socketConnection) {
189 VLOG(1) <<
"OnDisconnect " << client.getConfig().getInstanceName();
191 VLOG(1) <<
"\tLocal: " << socketConnection->getLocalAddress().toString();
192 VLOG(1) <<
"\tPeer: " << socketConnection->getRemoteAddress().toString();
static void init(int argc, char *argv[])
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
int main(int argc, char *argv[])
EchoSocketClient getClient()