Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 0 additions & 108 deletions src/cli/socket_utils.h

This file was deleted.

53 changes: 25 additions & 28 deletions src/cli/tls_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* 2017 René Korthaus, Rohde & Schwarz Cybersecurity
* 2022 René Meusel, Hannes Rantzsch - neXenio GmbH
* 2023 René Meusel, Rohde & Schwarz Cybersecurity
* 2025 Kagan Can Sit
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
Expand Down Expand Up @@ -31,8 +32,9 @@
#include <memory>
#include <string>

#include "socket_utils.h"
#include "tls_helpers.h"
#include <botan/internal/socket_platform.h>
#include <botan/internal/stl_util.h>

namespace Botan_CLI {

Expand Down Expand Up @@ -181,10 +183,10 @@ class TLS_Client final : public Command {
"--tls-version=default --session-db= --session-db-pass= "
"--next-protocols= --type=tcp --client-cert= --client-cert-key= "
"--psk= --psk-identity= --psk-prf=SHA-256 --debug") {
init_sockets();
Botan::OS::Socket_Platform::socket_init();
}

~TLS_Client() override { stop_sockets(); }
~TLS_Client() override { Botan::OS::Socket_Platform::socket_fini(); }

TLS_Client(const TLS_Client& other) = delete;
TLS_Client(TLS_Client&& other) = delete;
Expand Down Expand Up @@ -313,7 +315,8 @@ class TLS_Client final : public Command {
output() << "EOF on socket\n";
break;
} else if(got == -1) {
output() << "Socket error: " << errno << " " << err_to_string(errno) << "\n";
output() << "Socket error: " << errno << " " << Botan::OS::Socket_Platform::get_last_socket_error()
<< "\n";
continue;
}

Expand All @@ -334,7 +337,8 @@ class TLS_Client final : public Command {
we_closed = true;
break;
} else if(got == -1) {
output() << "Stdin error: " << errno << " " << err_to_string(errno) << "\n";
output() << "Stdin error: " << errno << " " << Botan::OS::Socket_Platform::get_last_socket_error()
<< "\n";
continue;
}

Expand Down Expand Up @@ -377,7 +381,8 @@ class TLS_Client final : public Command {
if(errno == EINTR) {
sent = 0;
} else {
throw CLI_Error("Socket write failed errno=" + std::to_string(errno));
throw CLI_Error("Socket write failed errno=" + std::to_string(errno) + " - " +
Botan::OS::Socket_Platform::get_last_socket_error());
}
}

Expand All @@ -386,55 +391,47 @@ class TLS_Client final : public Command {
}

private:
using socket_type = Botan::OS::Socket_Platform::socket_type;
using socket_op_ret_type = Botan::OS::Socket_Platform::socket_op_ret_type;
using socklen_type = Botan::OS::Socket_Platform::socklen_type;
using sendrecv_len_type = Botan::OS::Socket_Platform::sendrecv_len_type;

static socket_type connect_to_host(const std::string& host, uint16_t port, bool tcp) {
addrinfo hints{};
std::memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = tcp ? SOCK_STREAM : SOCK_DGRAM;
addrinfo* res = nullptr;

if(::getaddrinfo(host.c_str(), std::to_string(port).c_str(), &hints, &res) != 0) {
Botan::OS::Socket_Platform::unique_addrinfo_ptr res = nullptr;
if(::getaddrinfo(host.c_str(), std::to_string(port).c_str(), &hints, Botan::out_ptr(res)) != 0) {
throw CLI_Error("getaddrinfo failed for " + host);
}

socket_type fd = 0;
bool success = false;

for(addrinfo* rp = res; rp != nullptr; rp = rp->ai_next) {
fd = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
for(addrinfo* rp = res.get(); rp != nullptr; rp = rp->ai_next) {
socket_type fd = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);

if(fd == invalid_socket()) {
if(fd == Botan::OS::Socket_Platform::invalid_socket()) {
continue;
}

if(::connect(fd, rp->ai_addr, rp->ai_addrlen) != 0) {
::close(fd);
continue;
}

success = true;
break;
return fd;
}

::freeaddrinfo(res);

if(!success) {
// no address succeeded
throw CLI_Error("Connecting to host failed");
}

return fd;
throw CLI_Error("Connect failed");
}

static void dgram_socket_write(int sockfd, const uint8_t buf[], size_t length) {
auto r = ::send(sockfd, buf, length, MSG_NOSIGNAL);

if(r == -1) {
throw CLI_Error("Socket write failed errno=" + std::to_string(errno));
throw CLI_Error("Socket write failed errno=" + Botan::OS::Socket_Platform::get_last_socket_error());
}
}

socket_type m_sockfd = invalid_socket();
socket_type m_sockfd = Botan::OS::Socket_Platform::invalid_socket();
};

namespace {
Expand Down
36 changes: 22 additions & 14 deletions src/cli/tls_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* (C) 2014 Jack Lloyd
* 2017 René Korthaus, Rohde & Schwarz Cybersecurity
* 2023 René Meusel, Rohde & Schwarz Cybersecurity
2025 Kagan Can Sit
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
Expand Down Expand Up @@ -40,8 +41,8 @@
#include <list>
#include <memory>

#include "socket_utils.h"
#include "tls_helpers.h"
#include <botan/internal/socket_platform.h>

namespace Botan_CLI {

Expand Down Expand Up @@ -122,10 +123,10 @@ class TLS_Server final : public Command {
"tls_server cert-or-pubkey key --port=443 --psk= --psk-identity= --psk-prf=SHA-256 --type=tcp --policy=default --dump-traces= --max-clients=0")
#endif
{
init_sockets();
Botan::OS::Socket_Platform::socket_init();
}

~TLS_Server() override { stop_sockets(); }
~TLS_Server() override { Botan::OS::Socket_Platform::socket_fini(); }

TLS_Server(const TLS_Server& other) = delete;
TLS_Server(TLS_Server&& other) = delete;
Expand Down Expand Up @@ -239,7 +240,8 @@ class TLS_Server final : public Command {
ssize_t got = ::recv(m_socket, Botan::cast_uint8_ptr_to_char(buf), sizeof(buf), 0);

if(got == -1) {
error_output() << "Error in socket read - " << err_to_string(errno) << std::endl;
error_output() << "Error in socket read - "
<< Botan::OS::Socket_Platform::get_last_socket_error() << std::endl;
break;
}

Expand All @@ -266,8 +268,8 @@ class TLS_Server final : public Command {
} catch(std::exception& e) {
error_output() << "Connection problem: " << e.what() << std::endl;
if(m_is_tcp) {
close_socket(m_socket);
m_socket = invalid_socket();
Botan::OS::Socket_Platform::close_socket(m_socket);
m_socket = Botan::OS::Socket_Platform::invalid_socket();
}
}
}
Expand All @@ -276,12 +278,12 @@ class TLS_Server final : public Command {
}

if(m_is_tcp) {
close_socket(m_socket);
m_socket = invalid_socket();
Botan::OS::Socket_Platform::close_socket(m_socket);
m_socket = Botan::OS::Socket_Platform::invalid_socket();
}
}

close_socket(server_fd);
Botan::OS::Socket_Platform::close_socket(server_fd);
}

public:
Expand All @@ -293,7 +295,8 @@ class TLS_Server final : public Command {
ssize_t sent = ::send(m_socket, buf.data(), static_cast<sendrecv_len_type>(buf.size()), MSG_NOSIGNAL);

if(sent == -1) {
error_output() << "Error writing to socket - " << err_to_string(errno) << std::endl;
error_output() << "Error writing to socket - " << Botan::OS::Socket_Platform::get_last_socket_error()
<< std::endl;
} else if(sent >= 0 && static_cast<size_t>(sent) != buf.size()) {
error_output() << "Packet of length " << buf.size() << " truncated to " << sent << std::endl;
}
Expand All @@ -317,11 +320,16 @@ class TLS_Server final : public Command {
void push_pending_output(std::string line) { m_pending_output.emplace_back(std::move(line)); }

private:
using socket_type = Botan::OS::Socket_Platform::socket_type;
using socket_op_ret_type = Botan::OS::Socket_Platform::socket_op_ret_type;
using socklen_type = Botan::OS::Socket_Platform::socklen_type;
using sendrecv_len_type = Botan::OS::Socket_Platform::sendrecv_len_type;

socket_type make_server_socket(uint16_t port) {
const int type = m_is_tcp ? SOCK_STREAM : SOCK_DGRAM;

socket_type fd = ::socket(PF_INET, type, 0);
if(fd == invalid_socket()) {
if(fd == Botan::OS::Socket_Platform::invalid_socket()) {
throw CLI_Error("Unable to acquire socket");
}

Expand All @@ -334,14 +342,14 @@ class TLS_Server final : public Command {
socket_info.sin_addr.s_addr = INADDR_ANY;

if(::bind(fd, reinterpret_cast<struct sockaddr*>(&socket_info), sizeof(struct sockaddr)) != 0) {
close_socket(fd);
Botan::OS::Socket_Platform::close_socket(fd);
throw CLI_Error("server bind failed");
}

if(m_is_tcp) {
constexpr int backlog = std::min(100, SOMAXCONN);
if(::listen(fd, backlog) != 0) {
close_socket(fd);
Botan::OS::Socket_Platform::close_socket(fd);
throw CLI_Error("listen failed");
}
}
Expand All @@ -360,7 +368,7 @@ class TLS_Server final : public Command {
return fd;
}

socket_type m_socket = invalid_socket();
socket_type m_socket = Botan::OS::Socket_Platform::invalid_socket();
bool m_is_tcp = false;
uint32_t m_socket_id = 0;
std::list<std::string> m_pending_output;
Expand Down
1 change: 1 addition & 0 deletions src/lib/utils/socket/info.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ name -> "Socket"
uri.h
socket.h
socket_udp.h
socket_platform.h
</header:internal>

<libs>
Expand Down
Loading
Loading