]> git.meshlink.io Git - catta/blob - src/compat/windows/wincompat.h
wrap lifetimes of CattaServer and CattaSimplePoll in WSAStartup/WSACleanup
[catta] / src / compat / windows / wincompat.h
1 #ifndef foowincompatfoo
2 #define foowincompatfoo
3
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.
6
7
8 // require at least Windows Vista
9 #undef WINVER
10 #undef _WIN32_WINNT
11 #define WINVER 0x0600
12 #define _WIN32_WINNT WINVER
13
14 #include <winsock2.h>
15 #include <ws2tcpip.h>
16 #include <mswsock.h>
17
18
19 // wrappers around WSAStartup/WSACleanup to avoid clutter
20 void winsock_init(void);
21 void winsock_exit(void);
22
23
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
26 // latter.
27
28 struct iovec {                   /* Scatter/gather array items */
29    void  *iov_base;              /* Starting address */
30    size_t iov_len;               /* Number of bytes to transfer */
31 };
32
33 struct msghdr {
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 */
41 };
42
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
46 #ifdef __MINGW32__
47 #define cmsghdr _WSACMSGHDR     // as in 'struct cmsghdr'
48 #else
49 #define cmsghdr wsacmsghdr      // as in 'struct cmsghdr'
50 #endif
51
52 static inline struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *m) {
53     WSAMSG wm;
54     wm.Control.len = m->msg_controllen;
55     wm.Control.buf = m->msg_control;
56     return WSA_CMSG_FIRSTHDR(&wm);
57 }
58
59 static inline struct cmsghdr *CMSG_NXTHDR(struct msghdr *m, struct cmsghdr *c) {
60     WSAMSG wm;
61     wm.Control.len = m->msg_controllen;
62     wm.Control.buf = m->msg_control;
63     return WSA_CMSG_NXTHDR(&wm, c);
64 }
65
66 #define CMSG_SPACE(len) WSA_CMSG_SPACE(len)
67 #define CMSG_LEN(len) WSA_CMSG_LEN(len)
68
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.
71 #undef CMSG_DATA
72 #define CMSG_DATA(c) WSA_CMSG_DATA(c)
73
74 ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
75 ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
76
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
80 #ifndef ESHUTDOWN
81 #define ESHUTDOWN EBADF
82 #endif
83
84
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);
89
90
91 // Windows lacks poll, but WSAPoll is good enough for us.
92 #define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout)
93
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]);
97
98
99 // Windows logically doesn't have uname, so we supply a replacement.
100
101 struct utsname {
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 */
108 };
109
110 int uname(struct utsname *buf);
111
112
113 #endif