SNode.C
Loading...
Searching...
No Matches
servers.h
Go to the documentation of this file.
1/*
2 * SNode.C - A Slim Toolkit for Network Communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#ifndef APPS_ECHO_MODEL_SERVER_H
43#define APPS_ECHO_MODEL_SERVER_H
44
45#include "log/Logger.h"
46
47#define QUOTE_INCLUDE(a) STR_INCLUDE(a)
48#define STR_INCLUDE(a) #a
49
50// clang-format off
51#define SOCKETSERVER_INCLUDE QUOTE_INCLUDE(net/NET/stream/STREAM/SocketServer.h)
52// clang-format on
53
54#include SOCKETSERVER_INCLUDE // IWYU pragma: export
55
57
58#ifndef DOXYGEN_SHOULD_SKIP_THIS
59
60#include <string>
61
62#if (STREAM_TYPE == TLS) // tls
63#include <cstddef>
64#include <openssl/ssl.h>
65#include <openssl/x509v3.h>
66#endif
67
68#endif /* DOXYGEN_SHOULD_SKIP_THIS */
69
70#if (STREAM_TYPE == LEGACY) // legacy
71
72namespace apps::echo::model::legacy {
73
75
77 return EchoSocketServer("echoserver");
78 }
79
80} // namespace apps::echo::model::legacy
81
82#elif (STREAM_TYPE == TLS) // tls
83
84namespace apps::echo::model::tls {
85
86 using EchoSocketServer = net::NET::stream::tls::SocketServer<EchoServerSocketContextFactory>;
87 using SocketConnection = EchoSocketServer::SocketConnection;
88
89 EchoSocketServer getServer() {
90 EchoSocketServer server("echoserver");
91
92 server.setOnConnect([&server](SocketConnection* socketConnection) { // onConnect
93 VLOG(1) << "OnConnect " << server.getConfig().getInstanceName();
94
95 VLOG(1) << "\tLocal: " << socketConnection->getLocalAddress().toString();
96 VLOG(1) << "\tPeer: " << socketConnection->getRemoteAddress().toString();
97
98 /* Enable automatic hostname checks */
99 // X509_VERIFY_PARAM* param = SSL_get0_param(socketConnection->getSSL());
100
101 // X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
102 // if (!X509_VERIFY_PARAM_set1_host(param, "localhost", sizeof("localhost") - 1)) {
103 // // handle error
104 // socketConnection->close();
105 // }
106 });
107
108 server.setOnConnected([&server](SocketConnection* socketConnection) { // onConnected
109 VLOG(1) << "OnConnected " << server.getConfig().getInstanceName();
110
111 X509* server_cert = SSL_get_peer_certificate(socketConnection->getSSL());
112 if (server_cert != nullptr) {
113 long verifyErr = SSL_get_verify_result(socketConnection->getSSL());
114
115 VLOG(1) << "\tPeer certificate verifyErr = " + std::to_string(verifyErr) + ": " +
116 std::string(X509_verify_cert_error_string(verifyErr));
117
118 char* str = X509_NAME_oneline(X509_get_subject_name(server_cert), nullptr, 0);
119 VLOG(1) << "\t Subject: " + std::string(str);
120 OPENSSL_free(str);
121
122 str = X509_NAME_oneline(X509_get_issuer_name(server_cert), nullptr, 0);
123 VLOG(1) << "\t Issuer: " + std::string(str);
124 OPENSSL_free(str);
125
126 // We could do all sorts of certificate verification stuff here before deallocating the certificate.
127
128 GENERAL_NAMES* subjectAltNames =
129 static_cast<GENERAL_NAMES*>(X509_get_ext_d2i(server_cert, NID_subject_alt_name, nullptr, nullptr));
130#ifdef __GNUC__
131#pragma GCC diagnostic push
132#ifdef __has_warning
133#if __has_warning("-Wused-but-marked-unused")
134#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
135#endif
136#endif
137#endif
138 int32_t altNameCount = sk_GENERAL_NAME_num(subjectAltNames);
139#ifdef __GNUC_
140#pragma GCC diagnostic pop
141#endif
142 VLOG(1) << "\t Subject alternative name count: " << altNameCount;
143 for (int32_t i = 0; i < altNameCount; ++i) {
144#ifdef __GNUC__
145#pragma GCC diagnostic push
146#ifdef __has_warning
147#if __has_warning("-Wused-but-marked-unused")
148#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
149#endif
150#endif
151#endif
152 GENERAL_NAME* generalName = sk_GENERAL_NAME_value(subjectAltNames, i);
153#ifdef __GNUC_
154#pragma GCC diagnostic pop
155#endif
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;
166 } else {
167 VLOG(1) << "\t SAN (Type): '" + std::to_string(generalName->type);
168 }
169 }
170#ifdef __GNUC__
171#pragma GCC diagnostic push
172#ifdef __has_warning
173#if __has_warning("-Wused-but-marked-unused")
174#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
175#endif
176#endif
177#endif
178 sk_GENERAL_NAME_pop_free(subjectAltNames, GENERAL_NAME_free);
179#ifdef __GNUC_
180#pragma GCC diagnostic pop
181#endif
182 X509_free(server_cert);
183 } else {
184 VLOG(1) << "\tPeer certificate: no certificate";
185 }
186 });
187
188 server.setOnDisconnect([&server](SocketConnection* socketConnection) { // onDisconnect
189 VLOG(1) << "OnDisconnect " << server.getConfig().getInstanceName();
190
191 VLOG(1) << "\tLocal: " << socketConnection->getLocalAddress().toString();
192 VLOG(1) << "\tPeer: " << socketConnection->getRemoteAddress().toString();
193 });
194
195 return server;
196 }
197
198} // namespace apps::echo::model::tls
199
200#endif
201
202#endif // APPS_ECHO_MODEL_SERVER_H
static void init(int argc, char *argv[])
Definition SNodeC.cpp:54
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
Definition SNodeC.cpp:60
int main(int argc, char *argv[])
#define QUOTE_INCLUDE(a)
Definition clients.h:47
#define STR_INCLUDE(a)
Definition servers.h:48
EchoSocketServer getServer()
Definition servers.h:76
Definition clients.h:72