aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-12-23 07:33:40 -0500
committerJack Lloyd <[email protected]>2017-12-23 07:57:20 -0500
commitf40f25f17a2965ca03d42a17599ced5d5f6d46ab (patch)
tree88600f381b24f25e44048a6f4ef866dc7125f36a
parentffc1ac4da043f8fa9a59cc2d83f7fb0744340a14 (diff)
Add wrapper functions to clean up cli code using sockets
-rw-r--r--src/cli/socket_utils.h81
-rw-r--r--src/cli/tls_client.cpp58
-rw-r--r--src/cli/tls_server.cpp47
3 files changed, 88 insertions, 98 deletions
diff --git a/src/cli/socket_utils.h b/src/cli/socket_utils.h
new file mode 100644
index 000000000..a8e2a51a6
--- /dev/null
+++ b/src/cli/socket_utils.h
@@ -0,0 +1,81 @@
+/*
+* (C) 2014,2017 Jack Lloyd
+* 2017 René Korthaus, Rohde & Schwarz Cybersecurity
+*
+* Botan is released under the Simplified BSD License (see license.txt)
+*/
+
+#ifndef BOTAN_SOCKET_H_
+#define BOTAN_SOCKET_H_
+
+#include <botan/build.h>
+#include "cli_exceptions.h"
+
+#if defined(BOTAN_TARGET_OS_IS_WINDOWS)
+
+#include <winsock2.h>
+#include <WS2tcpip.h>
+
+typedef size_t ssize_t;
+
+#define STDIN_FILENO _fileno(stdin)
+
+inline void init_sockets()
+ {
+ WSAData wsa_data;
+ WORD wsa_version = MAKEWORD(2, 2);
+
+ if(::WSAStartup(wsa_version, &wsa_data) != 0)
+ {
+ throw Botan_CLI::CLI_Error("WSAStartup() failed: " + std::to_string(WSAGetLastError()));
+ }
+
+ if(LOBYTE(wsa_data.wVersion) != 2 || HIBYTE(wsa_data.wVersion) != 2)
+ {
+ ::WSACleanup();
+ throw Botan_CLI::CLI_Error("Could not find a usable version of Winsock.dll");
+ }
+ }
+
+inline void stop_sockets()
+ {
+ ::WSACleanup();
+ }
+
+inline int close(int fd)
+ {
+ return ::closesocket(fd);
+ }
+
+inline int read(int s, void* buf, size_t len)
+ {
+ return ::recv(s, reinterpret_cast<char*>(buf), static_cast<int>(len), 0);
+ }
+
+inline 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);
+ }
+
+#else
+
+#include <sys/types.h>
+#include <sys/time.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <unistd.h>
+#include <errno.h>
+#include <fcntl.h>
+
+inline void init_sockets() {}
+inline void stop_sockets() {}
+
+#endif
+
+#if !defined(MSG_NOSIGNAL)
+ #define MSG_NOSIGNAL 0
+#endif
+
+#endif
diff --git a/src/cli/tls_client.cpp b/src/cli/tls_client.cpp
index 8adc90139..ddc443614 100644
--- a/src/cli/tls_client.cpp
+++ b/src/cli/tls_client.cpp
@@ -25,43 +25,7 @@
#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>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <netdb.h>
-#include <unistd.h>
-#include <errno.h>
-#include <fcntl.h>
-#endif
-
-#if !defined(MSG_NOSIGNAL)
- #define MSG_NOSIGNAL 0
-#endif
-
+#include "socket_utils.h"
#include "credentials.h"
namespace Botan_CLI {
@@ -74,28 +38,12 @@ class TLS_Client final : public Command, public Botan::TLS::Callbacks
"--tls1.0 --tls1.1 --tls1.2 "
"--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
+ init_sockets();
}
~TLS_Client()
{
-#if defined(BOTAN_TARGET_OS_IS_WINDOWS)
- ::WSACleanup();
-#endif
+ stop_sockets();
}
void go() override
diff --git a/src/cli/tls_server.cpp b/src/cli/tls_server.cpp
index 02c15bf7b..6c0b049a2 100644
--- a/src/cli/tls_server.cpp
+++ b/src/cli/tls_server.cpp
@@ -14,35 +14,12 @@
#include <botan/tls_policy.h>
#include <botan/hex.h>
#include <botan/internal/os_utils.h>
-#include "credentials.h"
#include <list>
#include <fstream>
-#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>
-#include <netinet/in.h>
-#include <netdb.h>
-#include <unistd.h>
-#include <errno.h>
-#include <fcntl.h>
-#endif
-
-#if !defined(MSG_NOSIGNAL)
- #define MSG_NOSIGNAL 0
-#endif
+#include "credentials.h"
+#include "socket_utils.h"
namespace Botan_CLI {
@@ -51,28 +28,12 @@ class TLS_Server final : public Command
public:
TLS_Server() : Command("tls_server cert key --port=443 --type=tcp --policy= --dump-traces=")
{
-#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
+ init_sockets();
}
~TLS_Server()
{
-#if defined(BOTAN_TARGET_OS_IS_WINDOWS)
- ::WSACleanup();
-#endif
+ stop_sockets();
}
void go() override