diff options
| author | Brent Cook <busterb@gmail.com> | 2015-06-05 04:31:56 -0500 |
|---|---|---|
| committer | Brent Cook <bcook@openbsd.org> | 2015-06-05 04:50:18 -0500 |
| commit | b4a6a615134b59435efbecdd8c5b5407b6af8e8f (patch) | |
| tree | 7e1b645be3b352fab0021da3b0e43e78a423347a /crypto | |
| parent | 1d27b22e82ce00d27d0886c8488e4cbed1cb618e (diff) | |
| download | portable-b4a6a615134b59435efbecdd8c5b5407b6af8e8f.tar.gz portable-b4a6a615134b59435efbecdd8c5b5407b6af8e8f.tar.bz2 portable-b4a6a615134b59435efbecdd8c5b5407b6af8e8f.zip | |
refactor win32 shims into posix_win.c
this also adds a rename shim that allows overwrites
Diffstat (limited to 'crypto')
| -rw-r--r-- | crypto/Makefile.am | 4 | ||||
| -rw-r--r-- | crypto/compat/posix_win.c | 167 |
2 files changed, 171 insertions, 0 deletions
diff --git a/crypto/Makefile.am b/crypto/Makefile.am index ad26168..b996488 100644 --- a/crypto/Makefile.am +++ b/crypto/Makefile.am | |||
| @@ -65,6 +65,10 @@ if !HAVE_TIMINGSAFE_BCMP | |||
| 65 | libcompat_la_SOURCES += compat/timingsafe_bcmp.c | 65 | libcompat_la_SOURCES += compat/timingsafe_bcmp.c |
| 66 | endif | 66 | endif |
| 67 | 67 | ||
| 68 | if HOST_WIN | ||
| 69 | libcompat_la_SOURCES += compat/posix_win.c | ||
| 70 | endif | ||
| 71 | |||
| 68 | include Makefile.am.arc4random | 72 | include Makefile.am.arc4random |
| 69 | 73 | ||
| 70 | libcrypto_la_SOURCES = | 74 | libcrypto_la_SOURCES = |
diff --git a/crypto/compat/posix_win.c b/crypto/compat/posix_win.c new file mode 100644 index 0000000..5793e40 --- /dev/null +++ b/crypto/compat/posix_win.c | |||
| @@ -0,0 +1,167 @@ | |||
| 1 | /* | ||
| 2 | * Public domain | ||
| 3 | * | ||
| 4 | * BSD socket emulation code for Winsock2 | ||
| 5 | * File IO compatibility shims | ||
| 6 | * Brent Cook <bcook@openbsd.org> | ||
| 7 | */ | ||
| 8 | |||
| 9 | #define NO_REDEF_POSIX_FUNCTIONS | ||
| 10 | |||
| 11 | #include <windows.h> | ||
| 12 | #include <ws2tcpip.h> | ||
| 13 | |||
| 14 | #include <errno.h> | ||
| 15 | #include <stdio.h> | ||
| 16 | #include <string.h> | ||
| 17 | #include <unistd.h> | ||
| 18 | |||
| 19 | void | ||
| 20 | posix_perror(const char *s) | ||
| 21 | { | ||
| 22 | fprintf(stderr, "%s: %s\n", s, strerror(errno)); | ||
| 23 | } | ||
| 24 | |||
| 25 | FILE * | ||
| 26 | posix_fopen(const char *path, const char *mode) | ||
| 27 | { | ||
| 28 | if (strchr(mode, 'b') == NULL) { | ||
| 29 | char *bin_mode = NULL; | ||
| 30 | if (asprintf(&bin_mode, "%sb", mode) == -1) | ||
| 31 | return NULL; | ||
| 32 | FILE *f = fopen(path, bin_mode); | ||
| 33 | free(bin_mode); | ||
| 34 | return f; | ||
| 35 | } | ||
| 36 | |||
| 37 | return fopen(path, mode); | ||
| 38 | } | ||
| 39 | |||
| 40 | int | ||
| 41 | posix_rename(const char *oldpath, const char *newpath) | ||
| 42 | { | ||
| 43 | MoveFileEx(oldpath, newpath, MOVEFILE_REPLACE_EXISTING) ? 0 : -1; | ||
| 44 | } | ||
| 45 | |||
| 46 | static int | ||
| 47 | wsa_errno(int err) | ||
| 48 | { | ||
| 49 | switch (err) { | ||
| 50 | case WSAENOBUFS: | ||
| 51 | errno = ENOMEM; | ||
| 52 | break; | ||
| 53 | case WSAEACCES: | ||
| 54 | errno = EACCES; | ||
| 55 | break; | ||
| 56 | case WSANOTINITIALISED: | ||
| 57 | errno = EPERM; | ||
| 58 | break; | ||
| 59 | case WSAEHOSTUNREACH: | ||
| 60 | case WSAENETDOWN: | ||
| 61 | errno = EIO; | ||
| 62 | break; | ||
| 63 | case WSAEFAULT: | ||
| 64 | errno = EFAULT; | ||
| 65 | break; | ||
| 66 | case WSAEINTR: | ||
| 67 | errno = EINTR; | ||
| 68 | break; | ||
| 69 | case WSAEINVAL: | ||
| 70 | errno = EINVAL; | ||
| 71 | break; | ||
| 72 | case WSAEINPROGRESS: | ||
| 73 | errno = EINPROGRESS; | ||
| 74 | break; | ||
| 75 | case WSAEWOULDBLOCK: | ||
| 76 | errno = EAGAIN; | ||
| 77 | break; | ||
| 78 | case WSAEOPNOTSUPP: | ||
| 79 | errno = ENOTSUP; | ||
| 80 | break; | ||
| 81 | case WSAEMSGSIZE: | ||
| 82 | errno = EFBIG; | ||
| 83 | break; | ||
| 84 | case WSAENOTSOCK: | ||
| 85 | errno = ENOTSOCK; | ||
| 86 | break; | ||
| 87 | case WSAENOPROTOOPT: | ||
| 88 | errno = ENOPROTOOPT; | ||
| 89 | break; | ||
| 90 | case WSAECONNREFUSED: | ||
| 91 | errno = ECONNREFUSED; | ||
| 92 | break; | ||
| 93 | case WSAEAFNOSUPPORT: | ||
| 94 | errno = EAFNOSUPPORT; | ||
| 95 | break; | ||
| 96 | case WSAENETRESET: | ||
| 97 | case WSAENOTCONN: | ||
| 98 | case WSAECONNABORTED: | ||
| 99 | case WSAECONNRESET: | ||
| 100 | case WSAESHUTDOWN: | ||
| 101 | case WSAETIMEDOUT: | ||
| 102 | errno = EPIPE; | ||
| 103 | break; | ||
| 104 | } | ||
| 105 | return -1; | ||
| 106 | } | ||
| 107 | |||
| 108 | int | ||
| 109 | posix_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) | ||
| 110 | { | ||
| 111 | int rc = connect(sockfd, addr, addrlen); | ||
| 112 | if (rc == SOCKET_ERROR) | ||
| 113 | return wsa_errno(WSAGetLastError()); | ||
| 114 | return rc; | ||
| 115 | } | ||
| 116 | |||
| 117 | int | ||
| 118 | posix_close(int fd) | ||
| 119 | { | ||
| 120 | if (closesocket(fd) == SOCKET_ERROR) { | ||
| 121 | int err = WSAGetLastError(); | ||
| 122 | return err == WSAENOTSOCK ? | ||
| 123 | close(fd) : wsa_errno(err); | ||
| 124 | } | ||
| 125 | return 0; | ||
| 126 | } | ||
| 127 | |||
| 128 | ssize_t | ||
| 129 | posix_read(int fd, void *buf, size_t count) | ||
| 130 | { | ||
| 131 | ssize_t rc = recv(fd, buf, count, 0); | ||
| 132 | if (rc == SOCKET_ERROR) { | ||
| 133 | int err = WSAGetLastError(); | ||
| 134 | return err == WSAENOTSOCK ? | ||
| 135 | read(fd, buf, count) : wsa_errno(err); | ||
| 136 | } | ||
| 137 | return rc; | ||
| 138 | } | ||
| 139 | |||
| 140 | ssize_t | ||
| 141 | posix_write(int fd, const void *buf, size_t count) | ||
| 142 | { | ||
| 143 | ssize_t rc = send(fd, buf, count, 0); | ||
| 144 | if (rc == SOCKET_ERROR) { | ||
| 145 | int err = WSAGetLastError(); | ||
| 146 | return err == WSAENOTSOCK ? | ||
| 147 | write(fd, buf, count) : wsa_errno(err); | ||
| 148 | } | ||
| 149 | return rc; | ||
| 150 | } | ||
| 151 | |||
| 152 | int | ||
| 153 | posix_getsockopt(int sockfd, int level, int optname, | ||
| 154 | void *optval, socklen_t *optlen) | ||
| 155 | { | ||
| 156 | int rc = getsockopt(sockfd, level, optname, (char *)optval, optlen); | ||
| 157 | return rc == 0 ? 0 : wsa_errno(WSAGetLastError()); | ||
| 158 | |||
| 159 | } | ||
| 160 | |||
| 161 | int | ||
| 162 | posix_setsockopt(int sockfd, int level, int optname, | ||
| 163 | const void *optval, socklen_t optlen) | ||
| 164 | { | ||
| 165 | int rc = setsockopt(sockfd, level, optname, (char *)optval, optlen); | ||
| 166 | return rc == 0 ? 0 : wsa_errno(WSAGetLastError()); | ||
| 167 | } | ||
