From 1fcae215853ea7b66afcf17ef38d775a98d8b781 Mon Sep 17 00:00:00 2001 From: Elms Date: Tue, 13 Oct 2020 20:34:27 -0700 Subject: [PATCH] Refactor socket code to support win32 --- configure.ac | 2 +- examples/tls/tls_common.h | 41 +++++++++----------- src/tpm2_swtpm.c | 5 +-- wolftpm/tpm2_socket.h | 81 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 28 deletions(-) create mode 100644 wolftpm/tpm2_socket.h diff --git a/configure.ac b/configure.ac index 8fa080e..6c41a13 100644 --- a/configure.ac +++ b/configure.ac @@ -210,7 +210,7 @@ then AC_MSG_ERROR([Cannot enable swtpm or devtpm with windows API]) fi - AM_CFLAGS="$AM_CFLAGS -DWOLFTPM_WINAPI -DNO_WOLFSSL_SERVER -DNO_WOLFSSL_CLIENT" + AM_CFLAGS="$AM_CFLAGS -DWOLFTPM_WINAPI" fi diff --git a/examples/tls/tls_common.h b/examples/tls/tls_common.h index 09b0c1e..d4fb6c7 100644 --- a/examples/tls/tls_common.h +++ b/examples/tls/tls_common.h @@ -27,6 +27,8 @@ #if !defined(WOLFTPM2_NO_WRAPPER) && !defined(WOLFTPM2_NO_WOLFCRYPT) +#include + #include #include #include @@ -92,10 +94,6 @@ typedef struct SockIoCbCtx { #ifndef WOLFSSL_USER_IO /* socket includes */ -#include -#include -#include -#include static inline int SockIORecv(WOLFSSL* ssl, char* buff, int sz, void* ctx) { @@ -110,10 +108,8 @@ static inline int SockIORecv(WOLFSSL* ssl, char* buff, int sz, void* ctx) printf("IO RECEIVE ERROR: "); switch (errno) { - #if EAGAIN != EWOULDBLOCK - case EAGAIN: /* EAGAIN == EWOULDBLOCK on some systems, but not others */ - #endif - case EWOULDBLOCK: + case SOCKET_EAGAIN: + case SOCKET_EWOULDBLOCK: if (wolfSSL_get_using_nonblock(ssl)) { printf("would block\n"); return WOLFSSL_CBIO_ERR_WANT_READ; @@ -122,16 +118,16 @@ static inline int SockIORecv(WOLFSSL* ssl, char* buff, int sz, void* ctx) printf("socket timeout\n"); return WOLFSSL_CBIO_ERR_TIMEOUT; } - case ECONNRESET: + case SOCKET_ECONNRESET: printf("connection reset\n"); return WOLFSSL_CBIO_ERR_CONN_RST; - case EINTR: + case SOCKET_EINTR: printf("socket interrupted\n"); return WOLFSSL_CBIO_ERR_ISR; - case ECONNREFUSED: + case SOCKET_ECONNREFUSED: printf("connection refused\n"); return WOLFSSL_CBIO_ERR_WANT_READ; - case ECONNABORTED: + case SOCKET_ECONNABORTED: printf("connection aborted\n"); return WOLFSSL_CBIO_ERR_CONN_CLOSE; default: @@ -174,19 +170,17 @@ static inline int SockIOSend(WOLFSSL* ssl, char* buff, int sz, void* ctx) printf("IO SEND ERROR: "); switch (errno) { - #if EAGAIN != EWOULDBLOCK - case EAGAIN: /* EAGAIN == EWOULDBLOCK on some systems, but not others */ - #endif - case EWOULDBLOCK: + case SOCKET_EAGAIN: + case SOCKET_EWOULDBLOCK: printf("would block\n"); return WOLFSSL_CBIO_ERR_WANT_READ; - case ECONNRESET: + case SOCKET_ECONNRESET: printf("connection reset\n"); return WOLFSSL_CBIO_ERR_CONN_RST; - case EINTR: + case SOCKET_EINTR: printf("socket interrupted\n"); return WOLFSSL_CBIO_ERR_ISR; - case EPIPE: + case SOCKET_EPIPE: printf("socket EPIPE\n"); return WOLFSSL_CBIO_ERR_CONN_CLOSE; default: @@ -227,7 +221,8 @@ static inline int SetupSocketAndListen(SockIoCbCtx* sockIoCtx, word32 port) } /* allow reuse */ - if (setsockopt(sockIoCtx->listenFd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) { + if (setsockopt(sockIoCtx->listenFd, SOL_SOCKET, SO_REUSEADDR, + (void*)&optval, sizeof(optval)) == -1) { printf("setsockopt SO_REUSEADDR failed\n"); return -1; } @@ -251,7 +246,7 @@ static inline int SocketWaitClient(SockIoCbCtx* sockIoCtx) { int connd; struct sockaddr_in clientAddr; - socklen_t size = sizeof(clientAddr); + XSOCKLENT size = sizeof(clientAddr); if ((connd = accept(sockIoCtx->listenFd, (struct sockaddr*)&clientAddr, &size)) == -1) { printf("ERROR: failed to accept the connection\n\n"); @@ -303,11 +298,11 @@ static inline int SetupSocketAndConnect(SockIoCbCtx* sockIoCtx, const char* host static inline void CloseAndCleanupSocket(SockIoCbCtx* sockIoCtx) { if (sockIoCtx->fd != -1) { - close(sockIoCtx->fd); + CloseSocket(sockIoCtx->fd); sockIoCtx->fd = -1; } if (sockIoCtx->listenFd != -1) { - close(sockIoCtx->listenFd); + CloseSocket(sockIoCtx->listenFd); sockIoCtx->listenFd = -1; } } diff --git a/src/tpm2_swtpm.c b/src/tpm2_swtpm.c index 4495731..6d008fb 100644 --- a/src/tpm2_swtpm.c +++ b/src/tpm2_swtpm.c @@ -42,10 +42,7 @@ #include #include -#include -#include -#include - +#include #ifndef TPM2_SWTPM_HOST #define TPM2_SWTPM_HOST "localhost" diff --git a/wolftpm/tpm2_socket.h b/wolftpm/tpm2_socket.h new file mode 100644 index 0000000..7a3937c --- /dev/null +++ b/wolftpm/tpm2_socket.h @@ -0,0 +1,81 @@ +/* tpm2_socket.h + * + * Copyright (C) 2006-2020 wolfSSL Inc. + * + * This file is part of wolfTPM. + * + * wolfTPM is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfTPM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#ifndef _TPM2_SOCKET_H_ +#define _TPM2_SOCKET_H_ + +/* socket includes */ +#if defined(_WIN32) + #include + #define SOCKET_T SOCKET + + /* TODO: HACKY for win32 */ + #undef SOCKET_INVALID + #define SOCKET_INVALID 0xFFFFFFFF +#else + #include + #include + #include + + #define SOCKET_T int +#endif + +#ifdef USE_WINDOWS_API + #ifndef CloseSocket + #define CloseSocket(s) closesocket(s) + #endif +#else + #ifndef CloseSocket + #define CloseSocket(s) close(s) + #endif +#endif + +#ifndef XSOCKLENT + #ifdef _WIN32 + #define XSOCKLENT int + #else + #define XSOCKLENT socklen_t + #endif +#endif + +#ifdef _WIN32 + /* no epipe yet */ + #ifndef WSAEPIPE + #define WSAEPIPE -12345 + #endif + #define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK + #define SOCKET_EAGAIN WSAETIMEDOUT + #define SOCKET_ECONNRESET WSAECONNRESET + #define SOCKET_EINTR WSAEINTR + #define SOCKET_EPIPE WSAEPIPE + #define SOCKET_ECONNREFUSED WSAENOTCONN + #define SOCKET_ECONNABORTED WSAECONNABORTED +#else + #define SOCKET_EWOULDBLOCK EWOULDBLOCK + #define SOCKET_EAGAIN EAGAIN + #define SOCKET_ECONNRESET ECONNRESET + #define SOCKET_EINTR EINTR + #define SOCKET_EPIPE EPIPE + #define SOCKET_ECONNREFUSED ECONNREFUSED + #define SOCKET_ECONNABORTED ECONNABORTED +#endif /* USE_WINDOWS_API */ + +#endif /* _TPM2_SOCKET_H_ */