aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib
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/lib
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/lib')
-rw-r--r--src/lib/utils/http_util/http_util.cpp47
-rw-r--r--src/lib/utils/http_util/info.txt4
2 files changed, 50 insertions, 1 deletions
diff --git a/src/lib/utils/http_util/http_util.cpp b/src/lib/utils/http_util/http_util.cpp
index f714c1bca..36a689c97 100644
--- a/src/lib/utils/http_util/http_util.cpp
+++ b/src/lib/utils/http_util/http_util.cpp
@@ -1,6 +1,7 @@
/*
* Sketchy HTTP client
* (C) 2013,2016 Jack Lloyd
+* 2017 René Korthaus, Rohde & Schwarz Cybersecurity
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
@@ -22,11 +23,33 @@
#include <boost/asio.hpp>
#elif defined(BOTAN_TARGET_OS_HAS_SOCKETS)
+#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 write(int s, const char* buf, size_t len)
+ {
+ return ::send(s, reinterpret_cast<const char*>(buf), static_cast<int>(len), 0);
+ }
+
+typedef size_t ssize_t;
+#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <netinet/in.h>
+#endif
#else
//#warning "No network support enabled in http_util"
#endif
@@ -63,6 +86,22 @@ std::string http_transact(const std::string& hostname,
return oss.str();
#elif defined(BOTAN_TARGET_OS_HAS_SOCKETS)
+#if defined(BOTAN_TARGET_OS_IS_WINDOWS)
+ WSAData wsa_data;
+ WORD wsa_version = MAKEWORD(2, 2);
+
+ if (::WSAStartup(wsa_version, &wsa_data) != 0)
+ {
+ throw HTTP_Error("WSAStartup() failed: " + std::to_string(WSAGetLastError()));
+ }
+
+ if (LOBYTE(wsa_data.wVersion) != 2 || HIBYTE(wsa_data.wVersion) != 2)
+ {
+ ::WSACleanup();
+ throw HTTP_Error("Could not find a usable version of Winsock.dll");
+ }
+#endif
+
hostent* host_addr = ::gethostbyname(hostname.c_str());
uint16_t port = 80;
@@ -74,7 +113,13 @@ std::string http_transact(const std::string& hostname,
struct socket_raii {
socket_raii(int fd) : m_fd(fd) {}
- ~socket_raii() { ::close(m_fd); }
+ ~socket_raii()
+ {
+ ::close(m_fd);
+#if defined(BOTAN_TARGET_OS_IS_WINDOWS)
+ ::WSACleanup();
+#endif
+ }
int m_fd;
};
diff --git a/src/lib/utils/http_util/info.txt b/src/lib/utils/http_util/info.txt
index fe9fc3ea7..63e569f64 100644
--- a/src/lib/utils/http_util/info.txt
+++ b/src/lib/utils/http_util/info.txt
@@ -1,3 +1,7 @@
<defines>
HTTP_UTIL -> 20131128
</defines>
+
+<libs>
+windows -> Ws2_32.lib
+</libs> \ No newline at end of file