1 #ifndef foowincompatfoo
2 #define foowincompatfoo
4 // This file and its companion wincompat.c provide some Posix interfaces to
5 // Windows APIs so the rest of the code can keep using them.
8 // require at least Windows Vista
12 #define _WIN32_WINNT WINVER
19 // wrappers around WSAStartup/WSACleanup to avoid clutter
20 void winsock_init(void);
21 void winsock_exit(void);
24 // the equivalent of strerror(errno) for Windows sockets
25 char *errnostrsocket(void);
28 // Winsock doesn't have recvmsg/sendmsg but offers the same functionality
29 // with WSARecvMsg/WSASendMsg, so we implement the former in terms of the
32 struct iovec { /* Scatter/gather array items */
33 void *iov_base; /* Starting address */
34 size_t iov_len; /* Number of bytes to transfer */
38 void *msg_name; /* optional address */
39 socklen_t msg_namelen; /* size of address */
40 struct iovec *msg_iov; /* scatter/gather array */
41 size_t msg_iovlen; /* # elements in msg_iov */
42 void *msg_control; /* ancillary data, see below */
43 size_t msg_controllen; /* ancillary data buffer len */
44 int msg_flags; /* flags on received message */
47 // MSDN says this struct is called wsacmsghdr but MingW uses _WSACMSGHDR.
48 // TODO: Verify what it is on actual Windows.
49 // cf. http://msdn.microsoft.com/en-us/library/ms741645(v=vs.85).aspx
51 #define cmsghdr _WSACMSGHDR // as in 'struct cmsghdr'
53 #define cmsghdr wsacmsghdr // as in 'struct cmsghdr'
56 static inline struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *m) {
58 wm.Control.len = m->msg_controllen;
59 wm.Control.buf = m->msg_control;
60 return WSA_CMSG_FIRSTHDR(&wm);
63 static inline struct cmsghdr *CMSG_NXTHDR(struct msghdr *m, struct cmsghdr *c) {
65 wm.Control.len = m->msg_controllen;
66 wm.Control.buf = m->msg_control;
67 return WSA_CMSG_NXTHDR(&wm, c);
70 #define CMSG_SPACE(len) WSA_CMSG_SPACE(len)
71 #define CMSG_LEN(len) WSA_CMSG_LEN(len)
73 // we're going to be naughty and redefine CMSG_DATA as an alias even though it
74 // is also a constant defined in wincrypt.h which we don't care about.
76 #define CMSG_DATA(c) WSA_CMSG_DATA(c)
78 ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
79 ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
81 // ESHUTDOWN does not seem to exist on Windows, even though WSAESHUTDOWN does.
82 // MingW doesn't define it and MSDN doesn't list it, so we alias it to EBADF.
83 // cf. http://msdn.microsoft.com/en-us/library/5814770t.aspx
85 #define ESHUTDOWN EBADF
89 // Windows doesn't have ioctl but offers ioctlsocket for some socket-related
90 // functions. Unfortunately, argument types differ, so we implement a
91 // (restricted) wrapper.
92 int ioctl(int d, unsigned long request, int *p);
95 // Windows lacks poll, but WSAPoll is good enough for us.
96 #define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout)
98 // Windows lacks pipe. It has an equivalent CreatePipe but we really need
99 // something to give to WSAPoll, so we fake it with a local TCP socket. (ugh)
100 int pipe(int pipefd[2]);
102 // pipe(socket)-specific read/write/close equivalents
103 #define closepipe closesocket
104 #define writepipe(s,buf,len) send(s, buf, len, 0)
105 #define readpipe(s,buf,len) recv(s, buf, len, 0)
108 // Windows logically doesn't have uname, so we supply a replacement.
111 char sysname[9]; /* Operating system name (e.g., "Linux") */
112 char nodename[MAX_COMPUTERNAME_LENGTH+1];
113 /* Name within "some implementation-defined network" */
114 char release[9]; /* Operating system release (e.g., "2.6.28") */
115 char version[9]; /* Operating system version */
116 char machine[9]; /* Hardware identifier */
119 int uname(struct utsname *buf);