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 // Winsock doesn't have recvmsg/sendmsg but offers the same functionality
25 // with WSARecvMsg/WSASendMsg, so we implement the former in terms of the
28 struct iovec { /* Scatter/gather array items */
29 void *iov_base; /* Starting address */
30 size_t iov_len; /* Number of bytes to transfer */
34 void *msg_name; /* optional address */
35 socklen_t msg_namelen; /* size of address */
36 struct iovec *msg_iov; /* scatter/gather array */
37 size_t msg_iovlen; /* # elements in msg_iov */
38 void *msg_control; /* ancillary data, see below */
39 size_t msg_controllen; /* ancillary data buffer len */
40 int msg_flags; /* flags on received message */
43 // MSDN says this struct is called wsacmsghdr but MingW uses _WSACMSGHDR.
44 // TODO: Verify what it is on actual Windows.
45 // cf. http://msdn.microsoft.com/en-us/library/ms741645(v=vs.85).aspx
47 #define cmsghdr _WSACMSGHDR // as in 'struct cmsghdr'
49 #define cmsghdr wsacmsghdr // as in 'struct cmsghdr'
52 static inline struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *m) {
54 wm.Control.len = m->msg_controllen;
55 wm.Control.buf = m->msg_control;
56 return WSA_CMSG_FIRSTHDR(&wm);
59 static inline struct cmsghdr *CMSG_NXTHDR(struct msghdr *m, struct cmsghdr *c) {
61 wm.Control.len = m->msg_controllen;
62 wm.Control.buf = m->msg_control;
63 return WSA_CMSG_NXTHDR(&wm, c);
66 #define CMSG_SPACE(len) WSA_CMSG_SPACE(len)
67 #define CMSG_LEN(len) WSA_CMSG_LEN(len)
69 // we're going to be naughty and redefine CMSG_DATA as an alias even though it
70 // is also a constant defined in wincrypt.h which we don't care about.
72 #define CMSG_DATA(c) WSA_CMSG_DATA(c)
74 ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
75 ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
77 // ESHUTDOWN does not seem to exist on Windows, even though WSAESHUTDOWN does.
78 // MingW doesn't define it and MSDN doesn't list it, so we alias it to EBADF.
79 // cf. http://msdn.microsoft.com/en-us/library/5814770t.aspx
81 #define ESHUTDOWN EBADF
85 // Windows doesn't have ioctl but offers ioctlsocket for some socket-related
86 // functions. Unfortunately, argument types differ, so we implement a
87 // (restricted) wrapper.
88 int ioctl(int d, unsigned long request, int *p);
91 // Windows lacks poll, but WSAPoll is good enough for us.
92 #define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout)
94 // Windows lacks pipe. It has an equivalent CreatePipe but we really need
95 // something to give to WSAPoll, so we fake it with a local TCP socket. (ugh)
96 int pipe(int pipefd[2]);
99 // Windows logically doesn't have uname, so we supply a replacement.
102 char sysname[9]; /* Operating system name (e.g., "Linux") */
103 char nodename[MAX_COMPUTERNAME_LENGTH+1];
104 /* Name within "some implementation-defined network" */
105 char release[9]; /* Operating system release (e.g., "2.6.28") */
106 char version[9]; /* Operating system version */
107 char machine[9]; /* Hardware identifier */
110 int uname(struct utsname *buf);