aboutsummaryrefslogtreecommitdiff
path: root/crypto
diff options
context:
space:
mode:
authorBrent Cook <busterb@gmail.com>2015-06-05 04:31:56 -0500
committerBrent Cook <bcook@openbsd.org>2015-06-05 04:50:18 -0500
commitb4a6a615134b59435efbecdd8c5b5407b6af8e8f (patch)
tree7e1b645be3b352fab0021da3b0e43e78a423347a /crypto
parent1d27b22e82ce00d27d0886c8488e4cbed1cb618e (diff)
downloadportable-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.am4
-rw-r--r--crypto/compat/posix_win.c167
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
65libcompat_la_SOURCES += compat/timingsafe_bcmp.c 65libcompat_la_SOURCES += compat/timingsafe_bcmp.c
66endif 66endif
67 67
68if HOST_WIN
69libcompat_la_SOURCES += compat/posix_win.c
70endif
71
68include Makefile.am.arc4random 72include Makefile.am.arc4random
69 73
70libcrypto_la_SOURCES = 74libcrypto_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
19void
20posix_perror(const char *s)
21{
22 fprintf(stderr, "%s: %s\n", s, strerror(errno));
23}
24
25FILE *
26posix_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
40int
41posix_rename(const char *oldpath, const char *newpath)
42{
43 MoveFileEx(oldpath, newpath, MOVEFILE_REPLACE_EXISTING) ? 0 : -1;
44}
45
46static int
47wsa_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
108int
109posix_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
117int
118posix_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
128ssize_t
129posix_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
140ssize_t
141posix_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
152int
153posix_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
161int
162posix_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}