8#include <botan/internal/socket.h>
9#include <botan/exceptn.h>
10#include <botan/mem_ops.h>
13#if defined(BOTAN_HAS_BOOST_ASIO)
19 #define BOOST_ASIO_DISABLE_SERIAL_PORT
20 #include <boost/asio.hpp>
21 #include <boost/asio/system_timer.hpp>
23#elif defined(BOTAN_TARGET_OS_HAS_SOCKETS)
24 #include <sys/socket.h>
26 #include <netinet/in.h>
33#elif defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
41#if defined(BOTAN_HAS_BOOST_ASIO)
43class Asio_Socket
final :
public OS::Socket
46 Asio_Socket(
const std::string& hostname,
47 const std::string& service,
48 std::chrono::milliseconds timeout) :
49 m_timeout(timeout), m_timer(m_io), m_tcp(m_io)
51 m_timer.expires_from_now(m_timeout);
54 boost::asio::ip::tcp::resolver resolver(m_io);
55 boost::asio::ip::tcp::resolver::query query(hostname, service);
56 boost::asio::ip::tcp::resolver::iterator dns_iter = resolver.resolve(query);
58 boost::system::error_code ec = boost::asio::error::would_block;
60 auto connect_cb = [&ec](
const boost::system::error_code& e,
61 boost::asio::ip::tcp::resolver::iterator) { ec = e; };
63 boost::asio::async_connect(m_tcp, dns_iter, connect_cb);
65 while(ec == boost::asio::error::would_block)
71 throw boost::system::system_error(ec);
72 if(m_tcp.is_open() ==
false)
73 throw System_Error(
"Connection to host " + hostname +
" failed");
76 void write(
const uint8_t buf[],
size_t len)
override
78 m_timer.expires_from_now(m_timeout);
80 boost::system::error_code ec = boost::asio::error::would_block;
82 m_tcp.async_send(boost::asio::buffer(buf, len),
83 [&ec](boost::system::error_code e,
size_t) { ec = e; });
85 while(ec == boost::asio::error::would_block) { m_io.run_one(); }
89 throw boost::system::system_error(ec);
93 size_t read(uint8_t buf[],
size_t len)
override
95 m_timer.expires_from_now(m_timeout);
97 boost::system::error_code ec = boost::asio::error::would_block;
100 m_tcp.async_read_some(boost::asio::buffer(buf, len),
101 [&](boost::system::error_code cb_ec,
size_t cb_got) { ec = cb_ec; got = cb_got; });
103 while(ec == boost::asio::error::would_block) { m_io.run_one(); }
107 if(ec == boost::asio::error::eof)
109 throw boost::system::system_error(ec);
118 if(m_tcp.is_open() && m_timer.expires_at() < std::chrono::system_clock::now())
120 boost::system::error_code err;
124 m_timer.async_wait(std::bind(&Asio_Socket::check_timeout,
this));
127 const std::chrono::milliseconds m_timeout;
128 boost::asio::io_service m_io;
129 boost::asio::system_timer m_timer;
130 boost::asio::ip::tcp::socket m_tcp;
133#elif defined(BOTAN_TARGET_OS_HAS_SOCKETS) || defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
135class BSD_Socket
final :
public OS::Socket
138#if defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
139 typedef SOCKET socket_type;
140 typedef int socket_op_ret_type;
141 typedef int socklen_type;
142 typedef int sendrecv_len_type;
143 static socket_type invalid_socket() {
return INVALID_SOCKET; }
144 static void close_socket(socket_type s) { ::closesocket(s); }
145 static std::string get_last_socket_error() {
return std::to_string(::WSAGetLastError()); }
147 static bool nonblocking_connect_in_progress()
149 return (::WSAGetLastError() == WSAEWOULDBLOCK);
152 static void set_nonblocking(socket_type s)
154 u_long nonblocking = 1;
155 ::ioctlsocket(s, FIONBIO, &nonblocking);
158 static void socket_init()
161 WORD wsa_version = MAKEWORD(2, 2);
163 if (::WSAStartup(wsa_version, &wsa_data) != 0)
165 throw System_Error(
"WSAStartup() failed", WSAGetLastError());
168 if (LOBYTE(wsa_data.wVersion) != 2 || HIBYTE(wsa_data.wVersion) != 2)
171 throw System_Error(
"Could not find a usable version of Winsock.dll");
175 static void socket_fini()
180 typedef int socket_type;
181 typedef ssize_t socket_op_ret_type;
182 typedef socklen_t socklen_type;
183 typedef size_t sendrecv_len_type;
184 static socket_type invalid_socket() {
return -1; }
185 static void close_socket(socket_type s) { ::close(s); }
186 static std::string get_last_socket_error() { return ::strerror(errno); }
187 static bool nonblocking_connect_in_progress() {
return (errno == EINPROGRESS); }
188 static void set_nonblocking(socket_type s)
191 throw System_Error(
"Setting socket to non-blocking state failed", errno);
194 static void socket_init() {}
195 static void socket_fini() {}
199 BSD_Socket(
const std::string& hostname,
200 const std::string& service,
201 std::chrono::microseconds timeout) : m_timeout(timeout)
205 m_socket = invalid_socket();
209 hints.ai_family = AF_UNSPEC;
210 hints.ai_socktype = SOCK_STREAM;
213 int rc = ::getaddrinfo(hostname.c_str(), service.c_str(), &hints, &res);
217 throw System_Error(
"Name resolution failed for " + hostname, rc);
220 for(addrinfo* rp = res; (m_socket == invalid_socket()) && (rp !=
nullptr); rp = rp->ai_next)
222 if(rp->ai_family != AF_INET && rp->ai_family != AF_INET6)
225 m_socket = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
227 if(m_socket == invalid_socket())
233 set_nonblocking(m_socket);
235 int err = ::connect(m_socket, rp->ai_addr,
static_cast<socklen_type
>(rp->ai_addrlen));
240 if(nonblocking_connect_in_progress())
242 struct timeval timeout_tv = make_timeout_tv();
246 FD_SET(
static_cast<int>(m_socket), &write_set);
248 active = ::select(
static_cast<int>(m_socket + 1),
nullptr, &write_set,
nullptr, &timeout_tv);
252 int socket_error = 0;
253 socklen_t len =
sizeof(socket_error);
255 if(::getsockopt(m_socket, SOL_SOCKET, SO_ERROR,
reinterpret_cast<char*
>(&socket_error), &len) < 0)
256 throw System_Error(
"Error calling getsockopt", errno);
258 if(socket_error != 0)
267 close_socket(m_socket);
268 m_socket = invalid_socket();
276 if(m_socket == invalid_socket())
278 throw System_Error(
"Connecting to " + hostname +
279 " for service " + service +
" failed", errno);
285 close_socket(m_socket);
286 m_socket = invalid_socket();
290 void write(
const uint8_t buf[],
size_t len)
override
294 FD_SET(m_socket, &write_set);
296 size_t sent_so_far = 0;
297 while(sent_so_far != len)
299 struct timeval timeout = make_timeout_tv();
300 int active = ::select(
static_cast<int>(m_socket + 1),
nullptr, &write_set,
nullptr, &timeout);
303 throw System_Error(
"Timeout during socket write");
305 const size_t left = len - sent_so_far;
306 socket_op_ret_type sent = ::send(m_socket,
cast_uint8_ptr_to_char(&buf[sent_so_far]),
static_cast<sendrecv_len_type
>(left), 0);
308 throw System_Error(
"Socket write failed", errno);
310 sent_so_far +=
static_cast<size_t>(sent);
314 size_t read(uint8_t buf[],
size_t len)
override
318 FD_SET(m_socket, &read_set);
320 struct timeval timeout = make_timeout_tv();
321 int active = ::select(
static_cast<int>(m_socket + 1), &read_set,
nullptr,
nullptr, &timeout);
324 throw System_Error(
"Timeout during socket read");
326 socket_op_ret_type got = ::recv(m_socket,
cast_uint8_ptr_to_char(buf),
static_cast<sendrecv_len_type
>(len), 0);
329 throw System_Error(
"Socket read failed", errno);
331 return static_cast<size_t>(got);
335 struct timeval make_timeout_tv() const
338 tv.tv_sec =
static_cast<decltype(timeval::tv_sec)
>(m_timeout.count() / 1000000);
339 tv.tv_usec =
static_cast<decltype(timeval::tv_usec)
>(m_timeout.count() % 1000000);;
343 const std::chrono::microseconds m_timeout;
344 socket_type m_socket;
351std::unique_ptr<OS::Socket>
353 const std::string& service,
354 std::chrono::milliseconds timeout)
356#if defined(BOTAN_HAS_BOOST_ASIO)
357 return std::unique_ptr<OS::Socket>(
new Asio_Socket(hostname, service, timeout));
359#elif defined(BOTAN_TARGET_OS_HAS_SOCKETS) || defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
360 return std::unique_ptr<OS::Socket>(
new BSD_Socket(hostname, service, timeout));
367 return std::unique_ptr<Socket>();
#define BOTAN_UNUSED(...)
int(* final)(unsigned char *, CTX *)
std::string to_string(const BER_Object &obj)
std::unique_ptr< Socket > BOTAN_TEST_API open_socket(const std::string &hostname, const std::string &service, std::chrono::milliseconds timeout)
const char * cast_uint8_ptr_to_char(const uint8_t *b)
void clear_mem(T *ptr, size_t n)