diff options
author | Brian Smith <brian@briansmith.org> | 2015-01-28 09:32:08 +0300 |
---|---|---|
committer | Adam Langley <agl@google.com> | 2015-01-28 23:24:09 +0300 |
commit | 33970e6ce0de1a553bd57d5b42527cd95292acae (patch) | |
tree | 2829eaa1e43c4a55e6a98cad876a826090c2fed2 /tool | |
parent | 9a9a193388f0b144388bfbb62c4cc4a5c8fd82f9 (diff) |
Enable bssl client/s_client and server/s_server on Windows.
Change-Id: Iea9bd25176724b56ebb21bded6925f5d30176548
Reviewed-on: https://boringssl-review.googlesource.com/3071
Reviewed-by: Adam Langley <agl@google.com>
Diffstat (limited to 'tool')
-rw-r--r-- | tool/client.cc | 11 | ||||
-rw-r--r-- | tool/digest.cc | 1 | ||||
-rw-r--r-- | tool/server.cc | 10 | ||||
-rw-r--r-- | tool/tool.cc | 4 | ||||
-rw-r--r-- | tool/transport_common.cc | 42 | ||||
-rw-r--r-- | tool/transport_common.h | 2 |
6 files changed, 43 insertions, 27 deletions
diff --git a/tool/client.cc b/tool/client.cc index d73d4080..59c5fe3e 100644 --- a/tool/client.cc +++ b/tool/client.cc @@ -14,16 +14,12 @@ #include <openssl/base.h> -// TODO(davidben): bssl client does not work on Windows. -#if !defined(OPENSSL_WINDOWS) - #include <string> #include <vector> #include <errno.h> #include <stdlib.h> #include <sys/types.h> -#include <sys/socket.h> #include <openssl/err.h> #include <openssl/ssl.h> @@ -47,6 +43,10 @@ static const struct argument kArguments[] = { }; bool Client(const std::vector<std::string> &args) { + if (!InitSocketLibrary()) { + return false; + } + std::map<std::string, std::string> args_map; if (!ParseKeyValueArguments(&args_map, args, kArguments)) { @@ -98,6 +98,3 @@ bool Client(const std::vector<std::string> &args) { SSL_CTX_free(ctx); return ok; } - - -#endif // !OPENSSL_WINDOWS diff --git a/tool/digest.cc b/tool/digest.cc index 9c75d2d7..086d3a4e 100644 --- a/tool/digest.cc +++ b/tool/digest.cc @@ -37,6 +37,7 @@ #include <io.h> #define PATH_MAX MAX_PATH #define read _read +typedef int ssize_t; #endif #include <openssl/digest.h> diff --git a/tool/server.cc b/tool/server.cc index bc3739fc..120e450b 100644 --- a/tool/server.cc +++ b/tool/server.cc @@ -14,9 +14,6 @@ #include <openssl/base.h> -// TODO(davidben): bssl client does not work on Windows. -#if !defined(OPENSSL_WINDOWS) - #include <string> #include <vector> @@ -50,6 +47,10 @@ static const struct argument kArguments[] = { }; bool Server(const std::vector<std::string> &args) { + if (!InitSocketLibrary()) { + return false; + } + std::map<std::string, std::string> args_map; if (!ParseKeyValueArguments(&args_map, args, kArguments)) { @@ -106,6 +107,3 @@ bool Server(const std::vector<std::string> &args) { SSL_CTX_free(ctx); return ok; } - - -#endif // !OPENSSL_WINDOWS diff --git a/tool/tool.cc b/tool/tool.cc index 3b24be5a..36e3de9f 100644 --- a/tool/tool.cc +++ b/tool/tool.cc @@ -26,10 +26,8 @@ #endif -#if !defined(OPENSSL_WINDOWS) bool Client(const std::vector<std::string> &args); bool Server(const std::vector<std::string> &args); -#endif bool MD5Sum(const std::vector<std::string> &args); bool SHA1Sum(const std::vector<std::string> &args); bool SHA224Sum(const std::vector<std::string> &args); @@ -49,12 +47,10 @@ struct Tool { static const Tool kTools[] = { { "speed", Speed }, { "pkcs12", DoPKCS12 }, -#if !defined(OPENSSL_WINDOWS) { "client", Client }, { "s_client", Client }, { "server", Server }, { "s_server", Server }, -#endif { "md5sum", MD5Sum }, { "sha1sum", SHA1Sum }, { "sha224sum", SHA224Sum }, diff --git a/tool/transport_common.cc b/tool/transport_common.cc index 528dd3e6..c2b3e0c8 100644 --- a/tool/transport_common.cc +++ b/tool/transport_common.cc @@ -14,17 +14,12 @@ #include <openssl/base.h> -// TODO(davidben): bssl client does not work on Windows. -#if !defined(OPENSSL_WINDOWS) - #include <string> #include <vector> #include <errno.h> #include <stdlib.h> #include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> #if !defined(OPENSSL_WINDOWS) #include <arpa/inet.h> @@ -32,11 +27,19 @@ #include <netdb.h> #include <netinet/in.h> #include <sys/select.h> +#include <sys/socket.h> #include <unistd.h> #else +#define WIN32_LEAN_AND_MEAN // prevent conflicting defines of X509_* names +#define NOMINMAX +#include <io.h> #include <WinSock2.h> #include <WS2tcpip.h> typedef int socklen_t; +typedef int ssize_t; +#define read _read +#define write _write +#pragma comment(lib, "Ws2_32.lib") #endif #include <openssl/err.h> @@ -45,6 +48,24 @@ typedef int socklen_t; #include "internal.h" +#if !defined(OPENSSL_WINDOWS) +static int closesocket(int sock) { + return close(sock); +} +#endif + +bool InitSocketLibrary() { +#if defined(OPENSSL_WINDOWS) + WSADATA wsaData; + int err = WSAStartup(MAKEWORD(2, 2), &wsaData); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error %d\n", err); + return false; + } +#endif + return true; +} + // Connect sets |*out_sock| to be a socket connected to the destination given // in |hostname_and_port|, which should be of the form "www.example.com:123". // It returns true on success and false otherwise. @@ -140,7 +161,7 @@ bool Accept(int *out_sock, const std::string &port) { ok = true; out: - close(server_sock); + closesocket(server_sock); return ok; } @@ -158,7 +179,7 @@ bool SocketSetNonBlocking(int sock, bool is_non_blocking) { #if defined(OPENSSL_WINDOWS) u_long arg = is_non_blocking; - ok = 0 == ioctlsocket(sock, FIOBIO, &arg); + ok = 0 == ioctlsocket(sock, FIONBIO, &arg); #else int flags = fcntl(sock, F_GETFL, 0); if (flags < 0) { @@ -217,7 +238,11 @@ bool TransferData(SSL *ssl, int sock) { if (n == 0) { FD_CLR(0, &read_fds); stdin_open = false; +#if !defined(OPENSSL_WINDOWS) shutdown(sock, SHUT_WR); +#else + shutdown(sock, SD_SEND); +#endif continue; } else if (n < 0) { perror("read from stdin"); @@ -271,6 +296,3 @@ bool TransferData(SSL *ssl, int sock) { } } } - - -#endif // !OPENSSL_WINDOWS diff --git a/tool/transport_common.h b/tool/transport_common.h index 6b96659a..b7010158 100644 --- a/tool/transport_common.h +++ b/tool/transport_common.h @@ -18,6 +18,8 @@ #include <openssl/ssl.h> #include <string.h> +// InitSocketLibrary calls the Windows socket init functions, if needed. +bool InitSocketLibrary(); // Connect sets |*out_sock| to be a socket connected to the destination given // in |hostname_and_port|, which should be of the form "www.example.com:123". |