refactor win32 shims into posix_win.c
this also adds a rename shim that allows overwrites
This commit is contained in:
parent
1d27b22e82
commit
b4a6a61513
1
.gitignore
vendored
1
.gitignore
vendored
@ -116,6 +116,7 @@ include/openssl/*.he
|
|||||||
!/crypto/Makefile.am.*
|
!/crypto/Makefile.am.*
|
||||||
!/crypto/compat/arc4random.h
|
!/crypto/compat/arc4random.h
|
||||||
!/crypto/compat/b_win.c
|
!/crypto/compat/b_win.c
|
||||||
|
!/crypto/compat/posix_win.c
|
||||||
!/crypto/compat/bsd_asprintf.c
|
!/crypto/compat/bsd_asprintf.c
|
||||||
!/crypto/compat/ui_openssl_win.c
|
!/crypto/compat/ui_openssl_win.c
|
||||||
|
|
||||||
|
@ -65,6 +65,10 @@ if !HAVE_TIMINGSAFE_BCMP
|
|||||||
libcompat_la_SOURCES += compat/timingsafe_bcmp.c
|
libcompat_la_SOURCES += compat/timingsafe_bcmp.c
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
if HOST_WIN
|
||||||
|
libcompat_la_SOURCES += compat/posix_win.c
|
||||||
|
endif
|
||||||
|
|
||||||
include Makefile.am.arc4random
|
include Makefile.am.arc4random
|
||||||
|
|
||||||
libcrypto_la_SOURCES =
|
libcrypto_la_SOURCES =
|
||||||
|
167
crypto/compat/posix_win.c
Normal file
167
crypto/compat/posix_win.c
Normal file
@ -0,0 +1,167 @@
|
|||||||
|
/*
|
||||||
|
* Public domain
|
||||||
|
*
|
||||||
|
* BSD socket emulation code for Winsock2
|
||||||
|
* File IO compatibility shims
|
||||||
|
* Brent Cook <bcook@openbsd.org>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define NO_REDEF_POSIX_FUNCTIONS
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
|
#include <ws2tcpip.h>
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
void
|
||||||
|
posix_perror(const char *s)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s: %s\n", s, strerror(errno));
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE *
|
||||||
|
posix_fopen(const char *path, const char *mode)
|
||||||
|
{
|
||||||
|
if (strchr(mode, 'b') == NULL) {
|
||||||
|
char *bin_mode = NULL;
|
||||||
|
if (asprintf(&bin_mode, "%sb", mode) == -1)
|
||||||
|
return NULL;
|
||||||
|
FILE *f = fopen(path, bin_mode);
|
||||||
|
free(bin_mode);
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
return fopen(path, mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
posix_rename(const char *oldpath, const char *newpath)
|
||||||
|
{
|
||||||
|
MoveFileEx(oldpath, newpath, MOVEFILE_REPLACE_EXISTING) ? 0 : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
wsa_errno(int err)
|
||||||
|
{
|
||||||
|
switch (err) {
|
||||||
|
case WSAENOBUFS:
|
||||||
|
errno = ENOMEM;
|
||||||
|
break;
|
||||||
|
case WSAEACCES:
|
||||||
|
errno = EACCES;
|
||||||
|
break;
|
||||||
|
case WSANOTINITIALISED:
|
||||||
|
errno = EPERM;
|
||||||
|
break;
|
||||||
|
case WSAEHOSTUNREACH:
|
||||||
|
case WSAENETDOWN:
|
||||||
|
errno = EIO;
|
||||||
|
break;
|
||||||
|
case WSAEFAULT:
|
||||||
|
errno = EFAULT;
|
||||||
|
break;
|
||||||
|
case WSAEINTR:
|
||||||
|
errno = EINTR;
|
||||||
|
break;
|
||||||
|
case WSAEINVAL:
|
||||||
|
errno = EINVAL;
|
||||||
|
break;
|
||||||
|
case WSAEINPROGRESS:
|
||||||
|
errno = EINPROGRESS;
|
||||||
|
break;
|
||||||
|
case WSAEWOULDBLOCK:
|
||||||
|
errno = EAGAIN;
|
||||||
|
break;
|
||||||
|
case WSAEOPNOTSUPP:
|
||||||
|
errno = ENOTSUP;
|
||||||
|
break;
|
||||||
|
case WSAEMSGSIZE:
|
||||||
|
errno = EFBIG;
|
||||||
|
break;
|
||||||
|
case WSAENOTSOCK:
|
||||||
|
errno = ENOTSOCK;
|
||||||
|
break;
|
||||||
|
case WSAENOPROTOOPT:
|
||||||
|
errno = ENOPROTOOPT;
|
||||||
|
break;
|
||||||
|
case WSAECONNREFUSED:
|
||||||
|
errno = ECONNREFUSED;
|
||||||
|
break;
|
||||||
|
case WSAEAFNOSUPPORT:
|
||||||
|
errno = EAFNOSUPPORT;
|
||||||
|
break;
|
||||||
|
case WSAENETRESET:
|
||||||
|
case WSAENOTCONN:
|
||||||
|
case WSAECONNABORTED:
|
||||||
|
case WSAECONNRESET:
|
||||||
|
case WSAESHUTDOWN:
|
||||||
|
case WSAETIMEDOUT:
|
||||||
|
errno = EPIPE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
posix_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
|
||||||
|
{
|
||||||
|
int rc = connect(sockfd, addr, addrlen);
|
||||||
|
if (rc == SOCKET_ERROR)
|
||||||
|
return wsa_errno(WSAGetLastError());
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
posix_close(int fd)
|
||||||
|
{
|
||||||
|
if (closesocket(fd) == SOCKET_ERROR) {
|
||||||
|
int err = WSAGetLastError();
|
||||||
|
return err == WSAENOTSOCK ?
|
||||||
|
close(fd) : wsa_errno(err);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t
|
||||||
|
posix_read(int fd, void *buf, size_t count)
|
||||||
|
{
|
||||||
|
ssize_t rc = recv(fd, buf, count, 0);
|
||||||
|
if (rc == SOCKET_ERROR) {
|
||||||
|
int err = WSAGetLastError();
|
||||||
|
return err == WSAENOTSOCK ?
|
||||||
|
read(fd, buf, count) : wsa_errno(err);
|
||||||
|
}
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t
|
||||||
|
posix_write(int fd, const void *buf, size_t count)
|
||||||
|
{
|
||||||
|
ssize_t rc = send(fd, buf, count, 0);
|
||||||
|
if (rc == SOCKET_ERROR) {
|
||||||
|
int err = WSAGetLastError();
|
||||||
|
return err == WSAENOTSOCK ?
|
||||||
|
write(fd, buf, count) : wsa_errno(err);
|
||||||
|
}
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
posix_getsockopt(int sockfd, int level, int optname,
|
||||||
|
void *optval, socklen_t *optlen)
|
||||||
|
{
|
||||||
|
int rc = getsockopt(sockfd, level, optname, (char *)optval, optlen);
|
||||||
|
return rc == 0 ? 0 : wsa_errno(WSAGetLastError());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
posix_setsockopt(int sockfd, int level, int optname,
|
||||||
|
const void *optval, socklen_t optlen)
|
||||||
|
{
|
||||||
|
int rc = setsockopt(sockfd, level, optname, (char *)optval, optlen);
|
||||||
|
return rc == 0 ? 0 : wsa_errno(WSAGetLastError());
|
||||||
|
}
|
@ -15,35 +15,16 @@ int asprintf(char **str, const char *fmt, ...);
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <errno.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
static inline void
|
void posix_perror(const char *s);
|
||||||
posix_perror(const char *s)
|
FILE * posix_fopen(const char *path, const char *mode);
|
||||||
{
|
int posix_rename(const char *oldpath, const char *newpath);
|
||||||
fprintf(stderr, "%s: %s\n", s, strerror(errno));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
#ifndef NO_REDEF_POSIX_FUNCTIONS
|
||||||
#define perror(errnum) posix_perror(errnum)
|
#define perror(errnum) posix_perror(errnum)
|
||||||
|
|
||||||
static inline FILE *
|
|
||||||
posix_fopen(const char *path, const char *mode)
|
|
||||||
{
|
|
||||||
char *bin_mode = mode;
|
|
||||||
if (strchr(mode, 'b') == NULL) {
|
|
||||||
bin_mode = NULL;
|
|
||||||
if (asprintf(&bin_mode, "%sb", mode) == -1)
|
|
||||||
return NULL;
|
|
||||||
fprintf(stderr, "opening bin file %s\n", bin_mode);
|
|
||||||
}
|
|
||||||
|
|
||||||
FILE *f = fopen(path, bin_mode);
|
|
||||||
if (bin_mode != mode)
|
|
||||||
free(bin_mode);
|
|
||||||
return f;
|
|
||||||
}
|
|
||||||
|
|
||||||
#define fopen(path, mode) posix_fopen(path, mode)
|
#define fopen(path, mode) posix_fopen(path, mode)
|
||||||
|
#define rename(oldpath, newpath) posix_rename(oldpath, newpath)
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -19,142 +19,29 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static int
|
int posix_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
|
||||||
wsa_errno(int err)
|
|
||||||
{
|
|
||||||
switch (err) {
|
|
||||||
case WSAENOBUFS:
|
|
||||||
errno = ENOMEM;
|
|
||||||
break;
|
|
||||||
case WSAEACCES:
|
|
||||||
errno = EACCES;
|
|
||||||
break;
|
|
||||||
case WSANOTINITIALISED:
|
|
||||||
errno = EPERM;
|
|
||||||
break;
|
|
||||||
case WSAEHOSTUNREACH:
|
|
||||||
case WSAENETDOWN:
|
|
||||||
errno = EIO;
|
|
||||||
break;
|
|
||||||
case WSAEFAULT:
|
|
||||||
errno = EFAULT;
|
|
||||||
break;
|
|
||||||
case WSAEINTR:
|
|
||||||
errno = EINTR;
|
|
||||||
break;
|
|
||||||
case WSAEINVAL:
|
|
||||||
errno = EINVAL;
|
|
||||||
break;
|
|
||||||
case WSAEINPROGRESS:
|
|
||||||
errno = EINPROGRESS;
|
|
||||||
break;
|
|
||||||
case WSAEWOULDBLOCK:
|
|
||||||
errno = EAGAIN;
|
|
||||||
break;
|
|
||||||
case WSAEOPNOTSUPP:
|
|
||||||
errno = ENOTSUP;
|
|
||||||
break;
|
|
||||||
case WSAEMSGSIZE:
|
|
||||||
errno = EFBIG;
|
|
||||||
break;
|
|
||||||
case WSAENOTSOCK:
|
|
||||||
errno = ENOTSOCK;
|
|
||||||
break;
|
|
||||||
case WSAENOPROTOOPT:
|
|
||||||
errno = ENOPROTOOPT;
|
|
||||||
break;
|
|
||||||
case WSAECONNREFUSED:
|
|
||||||
errno = ECONNREFUSED;
|
|
||||||
break;
|
|
||||||
case WSAEAFNOSUPPORT:
|
|
||||||
errno = EAFNOSUPPORT;
|
|
||||||
break;
|
|
||||||
case WSAENETRESET:
|
|
||||||
case WSAENOTCONN:
|
|
||||||
case WSAECONNABORTED:
|
|
||||||
case WSAECONNRESET:
|
|
||||||
case WSAESHUTDOWN:
|
|
||||||
case WSAETIMEDOUT:
|
|
||||||
errno = EPIPE;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int
|
int posix_close(int fd);
|
||||||
posix_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
|
ssize_t posix_read(int fd, void *buf, size_t count);
|
||||||
{
|
|
||||||
int rc = connect(sockfd, addr, addrlen);
|
|
||||||
if (rc == SOCKET_ERROR)
|
|
||||||
return wsa_errno(WSAGetLastError());
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
ssize_t posix_write(int fd, const void *buf, size_t count);
|
||||||
|
|
||||||
|
int posix_getsockopt(int sockfd, int level, int optname,
|
||||||
|
void *optval, socklen_t *optlen);
|
||||||
|
|
||||||
|
int posix_setsockopt(int sockfd, int level, int optname,
|
||||||
|
const void *optval, socklen_t optlen);
|
||||||
|
|
||||||
|
#ifndef NO_REDEF_POSIX_FUNCTIONS
|
||||||
#define connect(sockfd, addr, addrlen) posix_connect(sockfd, addr, addrlen)
|
#define connect(sockfd, addr, addrlen) posix_connect(sockfd, addr, addrlen)
|
||||||
|
|
||||||
static inline int
|
|
||||||
posix_close(int fd)
|
|
||||||
{
|
|
||||||
if (closesocket(fd) == SOCKET_ERROR) {
|
|
||||||
int err = WSAGetLastError();
|
|
||||||
return err == WSAENOTSOCK ?
|
|
||||||
close(fd) : wsa_errno(err);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#define close(fd) posix_close(fd)
|
#define close(fd) posix_close(fd)
|
||||||
|
|
||||||
static inline ssize_t
|
|
||||||
posix_read(int fd, void *buf, size_t count)
|
|
||||||
{
|
|
||||||
ssize_t rc = recv(fd, buf, count, 0);
|
|
||||||
if (rc == SOCKET_ERROR) {
|
|
||||||
int err = WSAGetLastError();
|
|
||||||
return err == WSAENOTSOCK ?
|
|
||||||
read(fd, buf, count) : wsa_errno(err);
|
|
||||||
}
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
#define read(fd, buf, count) posix_read(fd, buf, count)
|
#define read(fd, buf, count) posix_read(fd, buf, count)
|
||||||
|
|
||||||
static inline ssize_t
|
|
||||||
posix_write(int fd, const void *buf, size_t count)
|
|
||||||
{
|
|
||||||
ssize_t rc = send(fd, buf, count, 0);
|
|
||||||
if (rc == SOCKET_ERROR) {
|
|
||||||
int err = WSAGetLastError();
|
|
||||||
return err == WSAENOTSOCK ?
|
|
||||||
write(fd, buf, count) : wsa_errno(err);
|
|
||||||
}
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
#define write(fd, buf, count) posix_write(fd, buf, count)
|
#define write(fd, buf, count) posix_write(fd, buf, count)
|
||||||
|
|
||||||
static inline int
|
|
||||||
posix_getsockopt(int sockfd, int level, int optname,
|
|
||||||
void *optval, socklen_t *optlen)
|
|
||||||
{
|
|
||||||
int rc = getsockopt(sockfd, level, optname, (char *)optval, optlen);
|
|
||||||
return rc == 0 ? 0 : wsa_errno(WSAGetLastError());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#define getsockopt(sockfd, level, optname, optval, optlen) \
|
#define getsockopt(sockfd, level, optname, optval, optlen) \
|
||||||
posix_getsockopt(sockfd, level, optname, optval, optlen)
|
posix_getsockopt(sockfd, level, optname, optval, optlen)
|
||||||
|
|
||||||
static inline int
|
|
||||||
posix_setsockopt(int sockfd, int level, int optname,
|
|
||||||
const void *optval, socklen_t optlen)
|
|
||||||
{
|
|
||||||
int rc = setsockopt(sockfd, level, optname, (char *)optval, optlen);
|
|
||||||
return rc == 0 ? 0 : wsa_errno(WSAGetLastError());
|
|
||||||
}
|
|
||||||
|
|
||||||
#define setsockopt(sockfd, level, optname, optval, optlen) \
|
#define setsockopt(sockfd, level, optname, optval, optlen) \
|
||||||
posix_setsockopt(sockfd, level, optname, optval, optlen)
|
posix_setsockopt(sockfd, level, optname, optval, optlen)
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user