aboutsummaryrefslogtreecommitdiff
path: root/include/win32netcompat.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/win32netcompat.h')
-rw-r--r--include/win32netcompat.h155
1 files changed, 147 insertions, 8 deletions
diff --git a/include/win32netcompat.h b/include/win32netcompat.h
index 3c716b0..51384d0 100644
--- a/include/win32netcompat.h
+++ b/include/win32netcompat.h
@@ -1,3 +1,10 @@
1/*
2 * Public domain
3 *
4 * BSD socket emulation code for Winsock2
5 * Brent Cook <bcook@openbsd.org>
6 */
7
1#ifndef LIBCRYPTOCOMPAT_WIN32NETCOMPAT_H 8#ifndef LIBCRYPTOCOMPAT_WIN32NETCOMPAT_H
2#define LIBCRYPTOCOMPAT_WIN32NETCOMPAT_H 9#define LIBCRYPTOCOMPAT_WIN32NETCOMPAT_H
3 10
@@ -5,17 +12,149 @@
5 12
6#include <ws2tcpip.h> 13#include <ws2tcpip.h>
7 14
8#ifndef SHUT_RDWR
9#define SHUT_RDWR SD_BOTH 15#define SHUT_RDWR SD_BOTH
10#endif 16#define SHUT_RD SD_RECEIVE
17#define SHUT_WR SD_SEND
11 18
12#ifndef SHUT_RD 19#include <errno.h>
13#define SHUT_RD SD_RECEIVE 20#include <unistd.h>
14#endif
15 21
16#ifndef SHUT_WR 22static int
17#define SHUT_WR SD_SEND 23wsa_errno(int err)
18#endif 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
84static inline int
85posix_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
86{
87 int rc = connect(sockfd, addr, addrlen);
88 if (rc == SOCKET_ERROR)
89 return wsa_errno(WSAGetLastError());
90 return rc;
91}
92
93#define connect(sockfd, addr, addrlen) posix_connect(sockfd, addr, addrlen)
94
95static inline int
96posix_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
106#define close(fd) posix_close(fd)
107
108static inline ssize_t
109posix_read(int fd, void *buf, size_t count)
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
120#define read(fd, buf, count) posix_read(fd, buf, count)
121
122static inline ssize_t
123posix_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)
135
136static inline int
137posix_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) \
146 posix_getsockopt(sockfd, level, optname, optval, optlen)
147
148static inline int
149posix_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) \
157 posix_setsockopt(sockfd, level, optname, optval, optlen)
19 158
20#endif 159#endif
21 160