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 | |
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
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | crypto/Makefile.am | 4 | ||||
-rw-r--r-- | crypto/compat/posix_win.c | 167 | ||||
-rw-r--r-- | include/stdio.h | 31 | ||||
-rw-r--r-- | include/win32netcompat.h | 137 |
5 files changed, 190 insertions, 150 deletions
@@ -116,6 +116,7 @@ include/openssl/*.he | |||
116 | !/crypto/Makefile.am.* | 116 | !/crypto/Makefile.am.* |
117 | !/crypto/compat/arc4random.h | 117 | !/crypto/compat/arc4random.h |
118 | !/crypto/compat/b_win.c | 118 | !/crypto/compat/b_win.c |
119 | !/crypto/compat/posix_win.c | ||
119 | !/crypto/compat/bsd_asprintf.c | 120 | !/crypto/compat/bsd_asprintf.c |
120 | !/crypto/compat/ui_openssl_win.c | 121 | !/crypto/compat/ui_openssl_win.c |
121 | 122 | ||
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 | } | ||
diff --git a/include/stdio.h b/include/stdio.h index db369c9..76bd9da 100644 --- a/include/stdio.h +++ b/include/stdio.h | |||
@@ -15,35 +15,16 @@ int asprintf(char **str, const char *fmt, ...); | |||
15 | #endif | 15 | #endif |
16 | 16 | ||
17 | #ifdef _WIN32 | 17 | #ifdef _WIN32 |
18 | #include <errno.h> | ||
19 | #include <string.h> | ||
20 | 18 | ||
21 | static inline void | 19 | void posix_perror(const char *s); |
22 | posix_perror(const char *s) | 20 | FILE * posix_fopen(const char *path, const char *mode); |
23 | { | 21 | int posix_rename(const char *oldpath, const char *newpath); |
24 | fprintf(stderr, "%s: %s\n", s, strerror(errno)); | ||
25 | } | ||
26 | 22 | ||
23 | #ifndef NO_REDEF_POSIX_FUNCTIONS | ||
27 | #define perror(errnum) posix_perror(errnum) | 24 | #define perror(errnum) posix_perror(errnum) |
28 | |||
29 | static inline FILE * | ||
30 | posix_fopen(const char *path, const char *mode) | ||
31 | { | ||
32 | char *bin_mode = mode; | ||
33 | if (strchr(mode, 'b') == NULL) { | ||
34 | bin_mode = NULL; | ||
35 | if (asprintf(&bin_mode, "%sb", mode) == -1) | ||
36 | return NULL; | ||
37 | fprintf(stderr, "opening bin file %s\n", bin_mode); | ||
38 | } | ||
39 | |||
40 | FILE *f = fopen(path, bin_mode); | ||
41 | if (bin_mode != mode) | ||
42 | free(bin_mode); | ||
43 | return f; | ||
44 | } | ||
45 | |||
46 | #define fopen(path, mode) posix_fopen(path, mode) | 25 | #define fopen(path, mode) posix_fopen(path, mode) |
26 | #define rename(oldpath, newpath) posix_rename(oldpath, newpath) | ||
27 | #endif | ||
47 | 28 | ||
48 | #endif | 29 | #endif |
49 | 30 | ||
diff --git a/include/win32netcompat.h b/include/win32netcompat.h index 51384d0..452cfba 100644 --- a/include/win32netcompat.h +++ b/include/win32netcompat.h | |||
@@ -19,142 +19,29 @@ | |||
19 | #include <errno.h> | 19 | #include <errno.h> |
20 | #include <unistd.h> | 20 | #include <unistd.h> |
21 | 21 | ||
22 | static int | 22 | int posix_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); |
23 | wsa_errno(int err) | ||
24 | { | ||
25 | switch (err) { | ||
26 | case WSAENOBUFS: | ||
27 | errno = ENOMEM; | ||
28 | break; | ||
29 | case WSAEACCES: | ||
30 | errno = EACCES; | ||
31 | break; | ||
32 | case WSANOTINITIALISED: | ||
33 | errno = EPERM; | ||
34 | break; | ||
35 | case WSAEHOSTUNREACH: | ||
36 | case WSAENETDOWN: | ||
37 | errno = EIO; | ||
38 | break; | ||
39 | case WSAEFAULT: | ||
40 | errno = EFAULT; | ||
41 | break; | ||
42 | case WSAEINTR: | ||
43 | errno = EINTR; | ||
44 | break; | ||
45 | case WSAEINVAL: | ||
46 | errno = EINVAL; | ||
47 | break; | ||
48 | case WSAEINPROGRESS: | ||
49 | errno = EINPROGRESS; | ||
50 | break; | ||
51 | case WSAEWOULDBLOCK: | ||
52 | errno = EAGAIN; | ||
53 | break; | ||
54 | case WSAEOPNOTSUPP: | ||
55 | errno = ENOTSUP; | ||
56 | break; | ||
57 | case WSAEMSGSIZE: | ||
58 | errno = EFBIG; | ||
59 | break; | ||
60 | case WSAENOTSOCK: | ||
61 | errno = ENOTSOCK; | ||
62 | break; | ||
63 | case WSAENOPROTOOPT: | ||
64 | errno = ENOPROTOOPT; | ||
65 | break; | ||
66 | case WSAECONNREFUSED: | ||
67 | errno = ECONNREFUSED; | ||
68 | break; | ||
69 | case WSAEAFNOSUPPORT: | ||
70 | errno = EAFNOSUPPORT; | ||
71 | break; | ||
72 | case WSAENETRESET: | ||
73 | case WSAENOTCONN: | ||
74 | case WSAECONNABORTED: | ||
75 | case WSAECONNRESET: | ||
76 | case WSAESHUTDOWN: | ||
77 | case WSAETIMEDOUT: | ||
78 | errno = EPIPE; | ||
79 | break; | ||
80 | } | ||
81 | return -1; | ||
82 | } | ||
83 | 23 | ||
84 | static inline int | 24 | int posix_close(int fd); |
85 | posix_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) | 25 | ssize_t posix_read(int fd, void *buf, size_t count); |
86 | { | ||
87 | int rc = connect(sockfd, addr, addrlen); | ||
88 | if (rc == SOCKET_ERROR) | ||
89 | return wsa_errno(WSAGetLastError()); | ||
90 | return rc; | ||
91 | } | ||
92 | 26 | ||
93 | #define connect(sockfd, addr, addrlen) posix_connect(sockfd, addr, addrlen) | 27 | ssize_t posix_write(int fd, const void *buf, size_t count); |
94 | |||
95 | static inline int | ||
96 | posix_close(int fd) | ||
97 | { | ||
98 | if (closesocket(fd) == SOCKET_ERROR) { | ||
99 | int err = WSAGetLastError(); | ||
100 | return err == WSAENOTSOCK ? | ||
101 | close(fd) : wsa_errno(err); | ||
102 | } | ||
103 | return 0; | ||
104 | } | ||
105 | 28 | ||
106 | #define close(fd) posix_close(fd) | 29 | int posix_getsockopt(int sockfd, int level, int optname, |
30 | void *optval, socklen_t *optlen); | ||
107 | 31 | ||
108 | static inline ssize_t | 32 | int posix_setsockopt(int sockfd, int level, int optname, |
109 | posix_read(int fd, void *buf, size_t count) | 33 | const void *optval, socklen_t optlen); |
110 | { | ||
111 | ssize_t rc = recv(fd, buf, count, 0); | ||
112 | if (rc == SOCKET_ERROR) { | ||
113 | int err = WSAGetLastError(); | ||
114 | return err == WSAENOTSOCK ? | ||
115 | read(fd, buf, count) : wsa_errno(err); | ||
116 | } | ||
117 | return rc; | ||
118 | } | ||
119 | 34 | ||
35 | #ifndef NO_REDEF_POSIX_FUNCTIONS | ||
36 | #define connect(sockfd, addr, addrlen) posix_connect(sockfd, addr, addrlen) | ||
37 | #define close(fd) posix_close(fd) | ||
120 | #define read(fd, buf, count) posix_read(fd, buf, count) | 38 | #define read(fd, buf, count) posix_read(fd, buf, count) |
121 | |||
122 | static inline ssize_t | ||
123 | posix_write(int fd, const void *buf, size_t count) | ||
124 | { | ||
125 | ssize_t rc = send(fd, buf, count, 0); | ||
126 | if (rc == SOCKET_ERROR) { | ||
127 | int err = WSAGetLastError(); | ||
128 | return err == WSAENOTSOCK ? | ||
129 | write(fd, buf, count) : wsa_errno(err); | ||
130 | } | ||
131 | return rc; | ||
132 | } | ||
133 | |||
134 | #define write(fd, buf, count) posix_write(fd, buf, count) | 39 | #define write(fd, buf, count) posix_write(fd, buf, count) |
135 | |||
136 | static inline int | ||
137 | posix_getsockopt(int sockfd, int level, int optname, | ||
138 | void *optval, socklen_t *optlen) | ||
139 | { | ||
140 | int rc = getsockopt(sockfd, level, optname, (char *)optval, optlen); | ||
141 | return rc == 0 ? 0 : wsa_errno(WSAGetLastError()); | ||
142 | |||
143 | } | ||
144 | |||
145 | #define getsockopt(sockfd, level, optname, optval, optlen) \ | 40 | #define getsockopt(sockfd, level, optname, optval, optlen) \ |
146 | posix_getsockopt(sockfd, level, optname, optval, optlen) | 41 | posix_getsockopt(sockfd, level, optname, optval, optlen) |
147 | |||
148 | static inline int | ||
149 | posix_setsockopt(int sockfd, int level, int optname, | ||
150 | const void *optval, socklen_t optlen) | ||
151 | { | ||
152 | int rc = setsockopt(sockfd, level, optname, (char *)optval, optlen); | ||
153 | return rc == 0 ? 0 : wsa_errno(WSAGetLastError()); | ||
154 | } | ||
155 | |||
156 | #define setsockopt(sockfd, level, optname, optval, optlen) \ | 42 | #define setsockopt(sockfd, level, optname, optval, optlen) \ |
157 | posix_setsockopt(sockfd, level, optname, optval, optlen) | 43 | posix_setsockopt(sockfd, level, optname, optval, optlen) |
44 | #endif | ||
158 | 45 | ||
159 | #endif | 46 | #endif |
160 | 47 | ||