Refactor socket code to support win32

pull/130/head
Elms 2020-10-13 20:34:27 -07:00
parent 375eda23d8
commit 1fcae21585
4 changed files with 101 additions and 28 deletions

View File

@ -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

View File

@ -27,6 +27,8 @@
#if !defined(WOLFTPM2_NO_WRAPPER) && !defined(WOLFTPM2_NO_WOLFCRYPT)
#include <wolftpm/tpm2_socket.h>
#include <examples/tpm_io.h>
#include <examples/tpm_test.h>
#include <examples/tls/tls_common.h>
@ -92,10 +94,6 @@ typedef struct SockIoCbCtx {
#ifndef WOLFSSL_USER_IO
/* socket includes */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
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;
}
}

View File

@ -42,10 +42,7 @@
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <wolftpm/tpm2_socket.h>
#ifndef TPM2_SWTPM_HOST
#define TPM2_SWTPM_HOST "localhost"

View File

@ -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 <winsock2.h>
#define SOCKET_T SOCKET
/* TODO: HACKY for win32 */
#undef SOCKET_INVALID
#define SOCKET_INVALID 0xFFFFFFFF
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#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_ */