aboutsummaryrefslogtreecommitdiffstats
path: root/src/cli
diff options
context:
space:
mode:
authorRené Korthaus <[email protected]>2017-08-04 10:46:04 +0200
committerRené Korthaus <[email protected]>2017-08-04 10:48:41 +0200
commitb717ec3d228719eb0401b2e61d717edca704c899 (patch)
tree3dda4a8404bed481f29369b74b46aeaa1b2222f4 /src/cli
parent8a29dc8209c6e93581075bbc4c39ff5bf0cdace5 (diff)
Add support for Windows sockets to http_util
Based on the work by @slicer4ever, adds support for Windows sockets to http_util. As a bonus, we get Windows support for tls_client and tls_server CLI.
Diffstat (limited to 'src/cli')
-rw-r--r--src/cli/tls_client.cpp52
-rw-r--r--src/cli/tls_server.cpp39
2 files changed, 88 insertions, 3 deletions
diff --git a/src/cli/tls_client.cpp b/src/cli/tls_client.cpp
index 642e60373..4625ca3f1 100644
--- a/src/cli/tls_client.cpp
+++ b/src/cli/tls_client.cpp
@@ -1,6 +1,7 @@
/*
* (C) 2014,2015 Jack Lloyd
* 2016 Matthias Gierlings
+* 2017 René Korthaus, Rohde & Schwarz Cybersecurity
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
@@ -21,6 +22,28 @@
#include <string>
#include <memory>
+#if defined(BOTAN_TARGET_OS_IS_WINDOWS)
+#include <winsock2.h>
+#include <WS2tcpip.h>
+
+int close(int fd)
+ {
+ return ::closesocket(fd);
+ }
+
+int read(int s, void* buf, size_t len)
+ {
+ return ::recv(s, reinterpret_cast<char*>(buf), static_cast<int>(len), 0);
+ }
+
+int send(int s, const uint8_t* buf, size_t len, int flags)
+ {
+ return ::send(s, reinterpret_cast<const char*>(buf), static_cast<int>(len), flags);
+ }
+
+#define STDIN_FILENO _fileno(stdin)
+typedef size_t ssize_t;
+#else
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
@@ -30,6 +53,7 @@
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
+#endif
#if !defined(MSG_NOSIGNAL)
#define MSG_NOSIGNAL 0
@@ -45,7 +69,31 @@ class TLS_Client final : public Command, public Botan::TLS::Callbacks
TLS_Client()
: Command("tls_client host --port=443 --print-certs --policy= "
"--tls1.0 --tls1.1 --tls1.2 "
- "--session-db= --session-db-pass= --next-protocols= --type=tcp") {}
+ "--session-db= --session-db-pass= --next-protocols= --type=tcp")
+ {
+#if defined(BOTAN_TARGET_OS_IS_WINDOWS)
+ WSAData wsa_data;
+ WORD wsa_version = MAKEWORD(2, 2);
+
+ if(::WSAStartup(wsa_version, &wsa_data) != 0)
+ {
+ throw CLI_Error("WSAStartup() failed: " + std::to_string(WSAGetLastError()));
+ }
+
+ if(LOBYTE(wsa_data.wVersion) != 2 || HIBYTE(wsa_data.wVersion) != 2)
+ {
+ ::WSACleanup();
+ throw CLI_Error("Could not find a usable version of Winsock.dll");
+ }
+#endif
+ }
+
+ ~TLS_Client()
+ {
+#if defined(BOTAN_TARGET_OS_IS_WINDOWS)
+ ::WSACleanup();
+#endif
+ }
void go() override
{
@@ -343,7 +391,7 @@ class TLS_Client final : public Command, public Botan::TLS::Callbacks
static void dgram_socket_write(int sockfd, const uint8_t buf[], size_t length)
{
- int r = send(sockfd, buf, length, MSG_NOSIGNAL);
+ int r = ::send(sockfd, buf, length, MSG_NOSIGNAL);
if(r == -1)
{
diff --git a/src/cli/tls_server.cpp b/src/cli/tls_server.cpp
index 41e131dce..22e592f29 100644
--- a/src/cli/tls_server.cpp
+++ b/src/cli/tls_server.cpp
@@ -1,6 +1,7 @@
/*
* TLS echo server using BSD sockets
* (C) 2014 Jack Lloyd
+* 2017 René Korthaus, Rohde & Schwarz Cybersecurity
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
@@ -15,6 +16,17 @@
#include <list>
+#if defined(BOTAN_TARGET_OS_IS_WINDOWS)
+#include <winsock2.h>
+#include <WS2tcpip.h>
+
+// definitions in tls_client.cpp
+int close(int fd);
+int read(int s, void* buf, size_t len);
+int send(int s, const uint8_t* buf, size_t len, int flags);
+
+typedef size_t ssize_t;
+#else
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
@@ -23,6 +35,7 @@
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
+#endif
#if !defined(MSG_NOSIGNAL)
#define MSG_NOSIGNAL 0
@@ -33,7 +46,31 @@ namespace Botan_CLI {
class TLS_Server final : public Command
{
public:
- TLS_Server() : Command("tls_server cert key --port=443 --type=tcp --policy=") {}
+ TLS_Server() : Command("tls_server cert key --port=443 --type=tcp --policy=")
+ {
+#if defined(BOTAN_TARGET_OS_IS_WINDOWS)
+ WSAData wsa_data;
+ WORD wsa_version = MAKEWORD(2, 2);
+
+ if(::WSAStartup(wsa_version, &wsa_data) != 0)
+ {
+ throw CLI_Error("WSAStartup() failed: " + std::to_string(WSAGetLastError()));
+ }
+
+ if(LOBYTE(wsa_data.wVersion) != 2 || HIBYTE(wsa_data.wVersion) != 2)
+ {
+ ::WSACleanup();
+ throw CLI_Error("Could not find a usable version of Winsock.dll");
+ }
+#endif
+ }
+
+ ~TLS_Server()
+ {
+#if defined(BOTAN_TARGET_OS_IS_WINDOWS)
+ ::WSACleanup();
+#endif
+ }
void go() override
{